【问题标题】:SqlException on ExecutenonQuery for WinCE ApplicationWinCE 应用程序的 ExecutenonQuery 上的 SqlException
【发布时间】:2018-01-18 08:04:02
【问题描述】:

目前我正在为windows CE开发一个程序, 与 Sql Server 有连接。 我能够获取数据,但是每当我尝试在数据库中添加数据时。它总是显示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

【问题讨论】:

  • SQL Insert Query Using C#的可能重复
  • 先生已经去那里了,仍然报同样的错误:(
  • 如果您想知道错误复制/粘贴带有参数的sql语句并在管理工作室中执行它以查看它是否在那里工作(我怀疑它会)。
  • 异常可能有内部异常,请发布整个异常,包括所有内部异常。
  • 它可以工作:/

标签: c# windows-ce


【解决方案1】:

textBox1.ToString() 可能返回类似"System.Windows.Forms.TextBox, Text: 1" 的内容。

改用Text 属性:

cmd.Parameters.Add("@id", textBox1.Text); 
cmd.Parameters.Add("@name", textBox2.Text); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多