Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我试图在sql server中插入一个表,但它给出了一个错误,即关键字';属于'; DBConnection objconn=newdbconnection(); objconn.connection()//呼叫连接 System.Data.SqlClient.SqlCommand com=new System.Data.SqlClient.SqlCommand(“tbl_srsdata”,objconn.con); com.CommandType=System.Data.CommandType.Text; 变量行=表1。项目; 文本块字段; 文本块类型; 文本块数据类型; 文本块透镜; TextBlock buis; int-id; id=Redirect.identity; Show(id.ToString()); for(int i=0;i_C# - Fatal编程技术网

C# 我试图在sql server中插入一个表,但它给出了一个错误,即关键字';属于'; DBConnection objconn=newdbconnection(); objconn.connection()//呼叫连接 System.Data.SqlClient.SqlCommand com=new System.Data.SqlClient.SqlCommand(“tbl_srsdata”,objconn.con); com.CommandType=System.Data.CommandType.Text; 变量行=表1。项目; 文本块字段; 文本块类型; 文本块数据类型; 文本块透镜; TextBlock buis; int-id; id=Redirect.identity; Show(id.ToString()); for(int i=0;i

C# 我试图在sql server中插入一个表,但它给出了一个错误,即关键字';属于'; DBConnection objconn=newdbconnection(); objconn.connection()//呼叫连接 System.Data.SqlClient.SqlCommand com=new System.Data.SqlClient.SqlCommand(“tbl_srsdata”,objconn.con); com.CommandType=System.Data.CommandType.Text; 变量行=表1。项目; 文本块字段; 文本块类型; 文本块数据类型; 文本块透镜; TextBlock buis; int-id; id=Redirect.identity; Show(id.ToString()); for(int i=0;i,c#,C#,我在最后一行中遇到错误。错误为: System.Data.dll中发生“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理。 其他信息:关键字“of”附近的语法不正确。 有人能帮我整理一下吗。请按如下方式更改命令文本 DBConnection objconn = new DBConnection(); objconn.connection(); //calling connection

我在最后一行中遇到错误。错误为: System.Data.dll中发生“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理。 其他信息:关键字“of”附近的语法不正确。
有人能帮我整理一下吗。

请按如下方式更改命令文本

                DBConnection objconn = new DBConnection();
              objconn.connection(); //calling connection   

              System.Data.SqlClient.SqlCommand com = new System.Data.SqlClient.SqlCommand("tbl_srsdata", objconn.con);
              com.CommandType = System.Data.CommandType.Text;
              var rows = Table1.Items;
     TextBlock field;
              TextBlock type;
              TextBlock datatype;
              TextBlock len;
              TextBlock buis;
              int id;
     id = Redirect.identity;
              MessageBox.Show(id.ToString());
              for (int i = 0; i < rows.Count; i++)
              {
     DataGridRow row = (DataGridRow)Table1.ItemContainerGenerator.ContainerFromIndex(i);
                  for (int j = 0; j < Table1.Columns.Count; j++)
                  {
                      field = Table1.Columns[j].GetCellContent(row) as TextBlock;
                      j++;
                      type = Table1.Columns[j].GetCellContent(row) as TextBlock;
                      j++;
                      datatype = Table1.Columns[j].GetCellContent(row) as TextBlock;
                      j++;
                      len = Table1.Columns[j].GetCellContent(row) as TextBlock;
                      j++;
                      buis = Table1.Columns[j].GetCellContent(row) as TextBlock;
                      j++;
    com.CommandText = "insert into tbl_srsdata(id,field,[type_of_control],datatype,length,other_buisness_rules) values( " + id + ", " + field.Text + "," + type.Text + "," + datatype.Text + "," + len.Text + "," + buis.Text + ");"; 
                      com.ExecuteNonQuery();
                  }
    }

将文本框中的文本集成到查询中是个坏主意

您必须添加文本作为参数:

    "insert into tbl_srsdata(id,field,[type_of_control],datatype,length,other_buisness_rules) values( " + id + ", '" + field.Text + "','" + type.Text + "','" + datatype.Text + "'," + len.Text + ",'" + buis.Text + "');"
将文本直接集成到查询中可能会导致错误和安全问题

我不知道你们程序的逻辑是什么。但我建议(可能是错误的)这样写:

com.CommandText = "insert into tbl_srsdata(id,field,[type_of_control],datatype,length,other_buisness_rules) values(@id,@field,@type,@datatype,@len,@buis)";
com.Parameters.AddWithValue("@id", id);
com.Parameters.AddWithValue("@field", field.Text);
com.Parameters.AddWithValue("@type", type.Text);
com.Parameters.AddWithValue("@datatype", datatype.Text);
com.Parameters.AddWithValue("@len", len.Text);
com.Parameters.AddWithValue("@buis", buis.Text);
com.ExecuteNonQuery();
if(!Table1.Items.Any())
返回;
字符串strInsertCmd=
插入tbl\u srsdata(id、字段、[控制类型]、数据类型、长度、其他业务规则)+
“值(@id、@field、@type、@datatype、@len、@buis)”;
使用(var com=new System.Data.SqlClient.SqlCommand(strInsertCmd,objconn.con))
{
变量行=表1。项目;
int-id;
id=Redirect.identity;
Show(id.ToString());
for(int i=0;i
发生这种情况的原因可能有很多。您本可以提供您试图插入的输入

无论如何,您必须检查输入数据:

  • INSERT
    数据中是否有非数字或文本的部分?如果是这样,字符串文本需要用单引号括起来(
  • 如果输入是字符串文字,请检查输入本身是否包含单引号。如果是,则需要使用两个单引号(
    '
    )对其进行转义

显示一个失败的com.CommandText示例-密钥将在其中-此外,您可能需要在文本字段周围加引号。如果您插入文本字段,您应该用引号将其括起来。您能告诉我在哪里用引号将其括起来吗?Deepansibansal-请参阅@DmitriyZapevalov提供的答案,这是编写ado.net的正确方法。在数据库调用中始终使用参数。还应始终使用块将数据库连接包装在
中,以确保在使用完后立即关闭它们。否则,您可能会遇到打开的连接,这些连接停留的时间超过了需要的时间,这可能会导致性能问题(除其他外)。尽管这可能是一个快速修复方法,但这绝对不是执行ado.net命令的推荐方法。您应该始终参数化查询以避免语法错误和sql注入攻击。您应该始终参数化查询以避免语法错误和sql注入攻击。如果传入的参数包含一个滴答/单引号,那么它也完全不考虑什么,它完全基于用户想要使用的内容。答案是她面临的问题。我认为你不会投反对票给正确答案@我想你的留言可能是一种增强。@AkashAmin-我的反对票决不是主观的。建议的解决方案并不能解决问题。如果
datatype.Text
包含字符串
“some'datatype”
,该怎么办?那么问题还是会出现,不是吗?在不使用参数化sql的情况下构建查询字符串是一种非常糟糕的做法,即使参数被转义和检查(它们也不在这个答案中),也不应建议将其作为答案。@Igor I tota
if (!Table1.Items.Any())
    return;

string strInsertCmd = 
    "insert into tbl_srsdata(id,field,[type_of_control],datatype,length,other_buisness_rules)"+
    "values(@id,@field,@type,@datatype,@len,@buis)";

using (var com = new System.Data.SqlClient.SqlCommand(strInsertCmd, objconn.con))
{
    var rows = Table1.Items;
    int id;
    id = Redirect.identity;
    MessageBox.Show(id.ToString());
    for (int i = 0; i < rows.Count; i++)
    {
        DataGridRow row = (DataGridRow)Table1.ItemContainerGenerator.ContainerFromIndex(i);
        var field = Table1.Columns[0].GetCellContent(row) as TextBlock;
        var type = Table1.Columns[1].GetCellContent(row) as TextBlock;
        var datatype = Table1.Columns[2].GetCellContent(row) as TextBlock;
        var len = Table1.Columns[3].GetCellContent(row) as TextBlock;
        var buis = Table1.Columns[4].GetCellContent(row) as TextBlock;

        com.Parameters.Clear();
        com.Parameters.AddWithValue("@id", id);
        com.Parameters.AddWithValue("@field", field.Text);
        com.Parameters.AddWithValue("@type", type.Text);
        com.Parameters.AddWithValue("@datatype", datatype.Text);
        com.Parameters.AddWithValue("@len", len.Text);
        com.Parameters.AddWithValue("@buis", buis.Text);
        com.ExecuteNonQuery();
    }
}