首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ExecuteNonQuery()出错;

ExecuteNonQuery()出错;
EN

Stack Overflow用户
提问于 2012-08-14 22:22:40
回答 3查看 1.2K关注 0票数 2

我从ExecuteNonQuery()得到以下错误

代码语言:javascript
复制
"String or binary data would be truncated. The statement has been terminated."

问题是要发送的变量比数据库中的变量大。我使用了以下代码:

代码语言:javascript
复制
      protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //GridViewRow row = GridView1.SelectedRow;
        GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
        var Username = (Label)row.FindControl("Label3");
        var Password = (Label)row.FindControl("Label4");
        var Email = (Label)row.FindControl("Label5");
       // var ID_Inscricao = ((Label)row.FindControl("Label1")).Text;
        var ID_Inscricao = ((Label)row.FindControl("Label1")).Text;


        SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["FormacaoConnectionString"].ToString());
        SqlCommand sqlComm = new SqlCommand();


        sqlComm = sqlConn.CreateCommand();
        sqlComm.Parameters.Add("@Username", SqlDbType.Text);
        sqlComm.Parameters.Add("@Password", SqlDbType.Text);
        sqlComm.Parameters.Add("@Email", SqlDbType.Text);
        sqlComm.Parameters.Add("@ID_Inscricao", SqlDbType.Int);

        sqlComm.Parameters["@Username"].Value = Convert.ToString(Username);
        sqlComm.Parameters["@Password"].Value = Convert.ToString(Password);
        sqlComm.Parameters["@Email"].Value = Convert.ToString(Email);
        sqlComm.Parameters["@ID_Inscricao"].Value = Convert.ToInt32(ID_Inscricao);

        string sql = "INSERT INTO Utilizadores (Username, Password, Email, ID_Inscricao) VALUES (@Username, @Password, @Email, @ID_Inscricao)";
        sqlComm.CommandText = sql;
        sqlConn.Open();
        sqlComm.ExecuteNonQuery();
        sqlConn.Close();
    }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-08-14 22:24:33

当您尝试将某些内容插入到对该列而言太长的列中时,就会发生这种情况。

例如,当您有一个列VARCHAR(10)并尝试插入值"ABCDEFGHIJKLMNOP"时。这将导致数据被截断并生成错误。

我不知道在您的情况下是哪一列,但它必须是UsernamePasswordEmail中的一个。

票数 6
EN

Stack Overflow用户

发布于 2012-08-14 22:27:54

问题是您尝试填充的数据库中的数据类型小于您传递的值之一。你需要检查你的每个参数@Username,@Password和@Email,看看哪一个比数据库数据类型大。然后,如果需要,您可以增加数据库中数据类型的大小,或者在发送之前修剪参数值。

票数 0
EN

Stack Overflow用户

发布于 2013-07-22 17:26:30

代码语言:javascript
复制
 Dim qry As String
        qry = "inserg into Table1 values('" & TextBox1.Text & "'+'" & TextBox2.Text & "'+'" & DropDownList1.Text & "'+'" & DropDownList2.Text & "'+'" & TextBox4.Text & "'+'" & TextBox5.Text & "'+'" & RadioButtonList1.Text & "'+'" & RadioButton1.Text & "'+'" & RadioButtonList2.Text & "'+'" & CheckBoxList1.Text & "'+'" & TextBox6.Text & "'+'" & TextBox7.Text & "'+'" & TextBox8.Text & "'+'" & TextBox9.Text & "'+'" & TextBox10.Text & "'+'" & RadioButtonList3.Text & "'"
        Dim cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\MCALab\My Documents\net.mdb")
        Dim cmd As New OleDbCommand(qry, cn)
        cn.Open()
        **cmd.ExecuteNonQuery()**this line have error please any one know mean debug it.

        cn.Close()
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11954406

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档