最后请注意:| | DataDirectory目录\ testconn.mdf到filename = C添加的ExecuteNonQuery 和后,我从文件名=改变了连接字符串之后。 ....... \ testconn.mdf。我的数据开始插入到我的表格中。感谢所有的帮助。我在哪里出了毛病此C#简单的SQL插入
虽然这个SQL INSERT我发现代码运行没有任何异常,但将数据库浏览和看节目表数据后的工作,数据未插入。最初我没有使用事务代码,但在这个网站上阅读这可能是它也没有例外但仍然没有实际插入到表中的问题。在逐步完成的过程中,我可以看到conn.Open()和connClose()语句后状态从打开到关闭的状态。另外最好的做法是有一个更清洁/更好的方式来写我的SqlConnection字符串以及我的SqlCommand字符串?谢谢我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Transactions;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
CommittableTransaction MASTER_TRANSACTION = new CommittableTransaction();
// 1. Instantiate the connection
SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\testconn.mdf;Integrated Security=True;User Instance=True");
try
{
// 2. Open the connection
conn.Open();
conn.EnlistTransaction(MASTER_TRANSACTION);
// 3. Pass the connection to a command object
SqlCommand cmd = new SqlCommand("INSERT INTO Client_Master(Client_ID, Client_First, Client_Last) VALUES('2', 'Joe', 'Shmoe')", conn);
MASTER_TRANSACTION.Commit();
}
finally
{
conn.Close();
}
}
}
}
什么是'Client_Master'的架构? – 2011-03-24 00:38:29