C#如何在SQL表中插入数据并将空字符串设置为0

C#如何在SQL表中插入数据并将空字符串设置为0,c#,mysql,C#,Mysql,我正在使用此sql代码将数据插入表中 "INSERT INTO electric VALUES('" + date.Text + "', '" + h1.Text + "', '" + h2.Text + "', '" + h22.Text + "', '" + h3.Text + "', '" + h4.Text + "', '" + h5.Text + "', '" + h6.Text + "', '" + h7.Text + "', '" + h8.Text + "', '" + h9.T

我正在使用此sql代码将数据插入表中

"INSERT INTO electric VALUES('" + date.Text + "', '" + h1.Text + "', '" + h2.Text + "', '" + h22.Text + "', '" + h3.Text + "', '" + h4.Text + "', '" + h5.Text + "', '" + h6.Text + "', '" + h7.Text + "', '" + h8.Text + "', '" + h9.Text + "', '" + h10.Text + "');"
有时我会将一些文本框留空,它们都是int。但它不会插入到表中。 我将列默认值设置为0,但仍然不插入。

请帮我解决这个问题。
感谢您尝试将空字符串(“”)存储到int字段。仅当从要插入的列表中跳过字段时,才会使用默认值


换句话说,在连接查询时,需要将值强制转换为整数。我不熟悉C#但从简短的搜索中,可能可以使用

尝试以下操作:

using(SqlConnection connection = new SqlConnection(yourConnectionString))
{
    String query = "INSERT INTO electric (h1, h2, h3, h4) VALUES (@h1, @h2, @h3, @h4)";

    using(SqlCommand command = new SqlCommand(query, connection))
    {
       int h1Data = 0;
       Int32.TryParse(h1TextBox.Text, out h1Data);

       int h2Data = 0;
       Int32.TryParse(h2TextBox.Text, out h2Data);

       int h3Data = 0;
       Int32.TryParse(h3TextBox.Text, out h3Data);

       int h4Data = 0;
       Int32.TryParse(h4TextBox.Text, out h4Data);

       command.Parameters.AddWithValue("@h1", h1Data);
       command.Parameters.AddWithValue("@h2", h2Data);
       command.Parameters.AddWithValue("@h3", h3Data);
       command.Parameters.AddWithValue("@h4", h4Data);

       connection.Open();
       int result = command.ExecuteNonQuery();

       // Check Error
       if(result < 0)
       {
          // Error
       }
    }
}
使用(SqlConnection=newsqlconnection(yourConnectionString))
{
String query=“插入电气(h1、h2、h3、h4)值(@h1、@h2、@h3、@h4)”;
使用(SqlCommand=newsqlcommand(查询、连接))
{
int h1Data=0;
Int32.TryParse(h1TextBox.Text,out h1数据);
int h2Data=0;
Int32.TryParse(h2TextBox.Text,out h2数据);
int h3Data=0;
Int32.TryParse(h3TextBox.Text,out h3Data);
int h4Data=0;
Int32.TryParse(h4TextBox.Text,out h4Data);
command.Parameters.AddWithValue(“@h1”,h1数据);
command.Parameters.AddWithValue(“@h2”,h2Data);
command.Parameters.AddWithValue(“@h3”,h3Data);
command.Parameters.AddWithValue(“@h4”,h4Data);
connection.Open();
int result=command.ExecuteNonQuery();
//检查错误
如果(结果<0)
{
//错误
}
}
}
解释:

using(SqlConnection connection = new SqlConnection(yourConnectionString))
{
    String query = "INSERT INTO electric (h1, h2, h3, h4) VALUES (@h1, @h2, @h3, @h4)";

    using(SqlCommand command = new SqlCommand(query, connection))
    {
       int h1Data = 0;
       Int32.TryParse(h1TextBox.Text, out h1Data);

       int h2Data = 0;
       Int32.TryParse(h2TextBox.Text, out h2Data);

       int h3Data = 0;
       Int32.TryParse(h3TextBox.Text, out h3Data);

       int h4Data = 0;
       Int32.TryParse(h4TextBox.Text, out h4Data);

       command.Parameters.AddWithValue("@h1", h1Data);
       command.Parameters.AddWithValue("@h2", h2Data);
       command.Parameters.AddWithValue("@h3", h3Data);
       command.Parameters.AddWithValue("@h4", h4Data);

       connection.Open();
       int result = command.ExecuteNonQuery();

       // Check Error
       if(result < 0)
       {
          // Error
       }
    }
}
textBoxs应该是ints,如您所说,如果没有发生错误

现在很简单,我们创建int-var并将文本框内容放入其中。。默认情况下,int-var等于零,如果textBox内容不是number,则使用tryParse,转换将不会完成,但int-var仍具有默认值,即零,并将插入数据库(空textBox或空字符串也会发生同样的情况)

您可能需要对其进行编辑以完全按照您的需要工作


我希望这对你有帮助。。祝你好运

学习使用参数,而不是模糊不清的查询字符串。理想的方法是使用参数化查询或使用适当的强制转换。在您的案例中,列是一个整数,您正在传递“”值,这将导致强制转换错误。指定默认值意味着,如果您在insert中不传递该列,它将创建一行,并将该列值作为默认值。因此,如果我使用了该参数,并且它得到了一个空值,那么它将设置为默认值,即0@vivekshayes,并在分配值时执行正确的强制转换或创建动态SQL。我将尝试这样做。谢谢你的帮助,非常感谢。我真的很感谢你对我的帮助。代码是有效的,问题仍然是一样的。我试图将Int32.TryParse更改为Convert.toInt32,但它也不起作用。我尝试了很多我能想到的方法,但仍然不起作用@EssamMohamed