WinCE 应用程序的 ExecutenonQuery 上的 SqlException
SqlException on ExecutenonQuery for WinCE Application
目前我正在为 windows CE 开发程序,
与 Sql 服务器有连接。
我能够获取数据,但每当我尝试在数据库中添加数据时。它总是显示 SqlException
这是我的代码(这只是一个原型>
string insert = string.Empty;
string data1 = textBox1.ToString(), data2 = textBox2.ToString();
string conSTR = "Data Source=<ipAdd>;" //server + "Initial Catalog=test;" database + "User id=testuser;" + "Password=;";
SqlConnection conn = new SqlConnection(conSTR);
insert = "Insert into Customers values(@id,@name);";
SqlCommand cmd = new SqlCommand(insert, conn);
cmd.Connection.Open();
cmd.Parameters.Add("@id", textBox1.ToString());
cmd.Parameters.Add("@name", textBox2.ToString());
cmd.ExecuteNonQuery();
cmd.Connection.Close();
特定异常
System.Data.SqlClient.SqlException was unhandled
Message="SqlException"
Class=16
LineNumber=1
Number=8152
Procedure=""
Server="LAPTOP-AEUVQN3B"
Source=".Net SqlClient Data Provider"
State=13
StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, TdsParserState state)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, TdsParserState state)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior run, SqlCommand cmdHandler, SqlDataReader dataStream)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at sqlSample.create.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
at System.Windows.Forms.Application.Run(Form fm)
at sqlSample.Program.Main()
下面是错误发生位置的图像
ErrorImage
textBox1.ToString()
可能 returns 类似于 "System.Windows.Forms.TextBox, Text: 1"
。
改用Text
属性:
cmd.Parameters.Add("@id", textBox1.Text);
cmd.Parameters.Add("@name", textBox2.Text);
目前我正在为 windows CE 开发程序, 与 Sql 服务器有连接。 我能够获取数据,但每当我尝试在数据库中添加数据时。它总是显示 SqlException
这是我的代码(这只是一个原型>
string insert = string.Empty;
string data1 = textBox1.ToString(), data2 = textBox2.ToString();
string conSTR = "Data Source=<ipAdd>;" //server + "Initial Catalog=test;" database + "User id=testuser;" + "Password=;";
SqlConnection conn = new SqlConnection(conSTR);
insert = "Insert into Customers values(@id,@name);";
SqlCommand cmd = new SqlCommand(insert, conn);
cmd.Connection.Open();
cmd.Parameters.Add("@id", textBox1.ToString());
cmd.Parameters.Add("@name", textBox2.ToString());
cmd.ExecuteNonQuery();
cmd.Connection.Close();
特定异常
System.Data.SqlClient.SqlException was unhandled
Message="SqlException"
Class=16
LineNumber=1
Number=8152
Procedure=""
Server="LAPTOP-AEUVQN3B"
Source=".Net SqlClient Data Provider"
State=13
StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, TdsParserState state)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, TdsParserState state)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior run, SqlCommand cmdHandler, SqlDataReader dataStream)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at sqlSample.create.button1_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
at System.Windows.Forms.Application.Run(Form fm)
at sqlSample.Program.Main()
下面是错误发生位置的图像
ErrorImage
textBox1.ToString()
可能 returns 类似于 "System.Windows.Forms.TextBox, Text: 1"
。
改用Text
属性:
cmd.Parameters.Add("@id", textBox1.Text);
cmd.Parameters.Add("@name", textBox2.Text);