【发布时间】:2011-07-21 18:25:27
【问题描述】:
最后说明:在我将连接字符串从 Filename=|DataDirectory|\testconn.mdf 更改为 Filename=c:\Documents.. 之后添加了 executeNonQuery AND。 ......\testconn.mdf。我的数据开始插入到我的表中。谢谢大家的帮助。
在使用这个 sql insert 时,我发现代码运行时没有任何异常,但是在进入数据库资源管理器并查看显示表数据之后,数据没有被插入。最初我没有使用事务代码,但在这个站点上阅读这可能是它也无异常运行但实际上仍然没有插入表的问题。在单步执行时,我可以看到在 conn.Open() 和 connClose() 语句之后状态从 open 变为 close 的位置。除了最佳实践之外,是否有一种更清洁/更好的方法来编写我的 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的架构是什么?