C# Int.TryParse仅向数据库传递布尔值

C# Int.TryParse仅向数据库传递布尔值,c#,winforms,visual-studio,int,tryparse,C#,Winforms,Visual Studio,Int,Tryparse,起初,我在将数据插入数据库时出错。错误是: “输入字符串的格式不正确” 但后来我用Int.TryParse将string转换成Int,它就成功了。但我现在面临的问题是,Int.TryParse只向数据库传递类似于1或0的布尔值 例如:如果我在文本框中写入34,然后单击“确定”插入文本框仅向数据库传递布尔值。i-e;1或0 有人能帮我吗?任何帮助都将不胜感激 这是我的密码: else{ try { conn.Open(); SqlCommand c

起初,我在将数据插入数据库时出错。错误是:

“输入字符串的格式不正确”

但后来我用
Int.TryParse
string
转换成
Int
,它就成功了。但我现在面临的问题是,
Int.TryParse
只向数据库传递类似于1或0的布尔值

例如:如果我在
文本框中写入34,然后单击“确定”插入<代码>文本框
仅向数据库传递布尔值。i-e;1或0

有人能帮我吗?任何帮助都将不胜感激

这是我的密码:

else{
    try
    {
        conn.Open();

        SqlCommand cmd = new SqlCommand("INSERT INTO ProductServices (ProductCode, ProductStatus, ProductCategory, ProductName, ProductDescription, ProductUnitPrice, ProductCost, ProductPCost, ProductPeck, ProductStock, ProductSWarning, ProductWarehouse, ProductPNotes, ProductImage) VALUES (@ProductCode, @ProductStatus, @ProductCategory, @ProductName, @ProductDescription, @ProductUnitPrice, @ProductCost, @ProductPCost, @ProductPeck, @ProductStock, @ProductSWarning, @ProductWarehouse, @ProductPNotes, @ProductImage)", conn);

        //Save image from PictureBox into MemoryStream object.
        MemoryStream ms = new MemoryStream();
        pictureBoxProduct.Image.Save(ms, ImageFormat.Jpeg);

        //Read from MemoryStream into Byte array.
        Byte[] bytBLOBData = new Byte[ms.Length];
        ms.Position = 0;
        ms.Read(bytBLOBData, 0, Convert.ToInt32(ms.Length));

        SqlParameter[] param = new SqlParameter[14];

        //Create parameter for insert statement.
        param[0] = new SqlParameter("@ProductCode", SqlDbType.NVarChar, 255);
        param[1] = new SqlParameter("@ProductStatus", SqlDbType.NVarChar, 10);
        param[2] = new SqlParameter("@ProductCategory", SqlDbType.NVarChar, 100);
        param[3] = new SqlParameter("@ProductName", SqlDbType.NVarChar, 255);
        param[4] = new SqlParameter("@ProductDescription", SqlDbType.NVarChar, 255);
        param[5] = new SqlParameter("@ProductUnitPrice", SqlDbType.BigInt);
        param[6] = new SqlParameter("@ProductCost", SqlDbType.BigInt);
        param[7] = new SqlParameter("@ProductPCost", SqlDbType.BigInt);
        param[8] = new SqlParameter("@ProductPeck", SqlDbType.NVarChar, 50);
        param[9] = new SqlParameter("@ProductStock", SqlDbType.BigInt);
        param[10] = new SqlParameter("@ProductSWarning", SqlDbType.BigInt);
        param[11] = new SqlParameter("@ProductWarehouse", SqlDbType.NVarChar, 255);
        param[12] = new SqlParameter("@ProductPNotes", SqlDbType.Text);

        param[13] = new SqlParameter("@ProductImage", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, false,
        0, 0, null, DataRowVersion.Current, bytBLOBData);

        int unit; int cost; int pc; int stock; int war;
        param[0].Value = txtCode.Text;
        param[1].Value = ComboStatus.Text;
        param[2].Value = ComboCategory.Text;
        param[3].Value = txtName.Text;
        param[4].Value = txtDescription.Text;
        param[5].Value = Int32.TryParse(txtUnit.Text, out unit);
        param[6].Value = Int32.TryParse(txtCost.Text, out cost);
        param[7].Value = Int32.TryParse(txtPC.Text, out pc);
        param[8].Value = txtWeight.Text;
        param[9].Value = Int32.TryParse(txtStock.Text, out stock);
        param[10].Value = Int32.TryParse(txtWarhouse.Text, out war);
        param[11].Value = txtWarhouse.Text;
        param[12].Value = txtNotes.Text;

        for (int j = 0; j < param.Length; j++)
        {
            cmd.Parameters.Add(param[j]);
        }

        cmd.CommandType = CommandType.Text;
        cmd.ExecuteNonQuery();

        bmsmf1.loadProductServicesTable();
        this.Hide();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        conn.Close();
    }
}
else{
尝试
{
conn.Open();
SqlCommand cmd=新的SqlCommand(“插入到ProductServices(ProductCode、ProductStatus、ProductCategory、ProductName、ProductDescription、ProductUnitPrice、ProductCost、ProductPCost、ProductPeck、ProductStock、ProductSwarming、ProductWarehouse、ProductPNotes、ProductImage)值中(@ProductCode、@ProductStatus、@ProductCategory、@ProductName、@ProductDescription、@ProductUnitPrice、@ProductCost、@ProductPCost、@ProductPeck、@ProductStock、@productswering、@ProductWarehouse、@ProductPNotes、@ProductImage)”,康涅狄格州);
//将图像从PictureBox保存到MemoryStream对象中。
MemoryStream ms=新的MemoryStream();
pictureBoxProduct.Image.Save(ms,ImageFormat.Jpeg);
//从MemoryStream读入字节数组。
Byte[]bytBLOBData=新字节[ms.Length];
ms.Position=0;
ms.Read(bytBLOBData,0,转换为32(ms.Length));
SqlParameter[]param=新的SqlParameter[14];
//为insert语句创建参数。
param[0]=新的SqlParameter(“@ProductCode”,SqlDbType.NVarChar,255);
param[1]=新的SqlParameter(“@ProductStatus”,SqlDbType.NVarChar,10);
param[2]=新的SqlParameter(“@ProductCategory”,SqlDbType.NVarChar,100);
param[3]=新的SqlParameter(“@ProductName”,SqlDbType.NVarChar,255);
param[4]=新的SqlParameter(“@ProductDescription”,SqlDbType.NVarChar,255);
param[5]=新的SqlParameter(“@ProductUnitPrice”,SqlDbType.BigInt);
param[6]=新的SqlParameter(“@ProductCost”,SqlDbType.BigInt);
param[7]=新的SqlParameter(“@ProductPCost”,SqlDbType.BigInt);
param[8]=新的SqlParameter(“@ProductPeck”,SqlDbType.NVarChar,50);
param[9]=新的SqlParameter(“@ProductStock”,SqlDbType.BigInt);
param[10]=新的SqlParameter(“@ProductSWarning”,SqlDbType.BigInt);
param[11]=新的SqlParameter(“@ProductWarehouse”,SqlDbType.NVarChar,255);
param[12]=新的SqlParameter(“@ProductPNotes”,SqlDbType.Text);
param[13]=新的SqlParameter(“@ProductImage”,SqlDbType.VarBinary,bytBLOBData.Length,ParameterDirection.Input,false,
0,0,null,DataRowVersion.Current,bytBLOBData);
整数单位;整数成本;整数pc;整数库存;整数战争;
参数[0]。值=txtCode.Text;
参数[1]。值=ComboStatus.Text;
参数[2]。值=ComboCategory.Text;
param[3]。Value=txtName.Text;
param[4]。Value=txtDescription.Text;
param[5]。Value=Int32.TryParse(txtUnit.Text,输出单位);
param[6]。Value=Int32.TryParse(txtCost.Text,out-cost);
param[7]。Value=Int32.TryParse(txtPC.Text,out-pc);
param[8]。Value=txtwweight.Text;
参数[9]。值=Int32.TryParse(txtStock.Text,缺货);
param[10].Value=Int32.TryParse(txtWarhouse.Text,out-war);
param[11].Value=txtWarhouse.Text;
param[12]。Value=txtNotes.Text;
对于(int j=0;j
获取布尔值的原因是传递布尔值。
int.TryParse(字符串s,out x)
将返回True或False。如果要访问已解析的
int
值,则需要使用在
out
部分中指定的
x
变量。

问题是您正在将字段的值设置为返回布尔值的
int.TryParse()
方法的返回结果

只需将值设置为out中返回的值

i、 e


快速回答:您的代码被设置为使用
int.Parse(string)
替换它将使其工作正常

int.TryParse(string,out int)
是一个函数,用于确保当Int32无法表示字符串时,该字符串不会返回默认值(因为int不是可为null的类型)。如果TryParse返回true,则字符串已成功转换为int

TryParse示例:

int num;
if (int.TryParse("1", out num))
{
    Console.WriteLine(num);
}
解析示例:

int num = int.Parse("1");

请参见此示例:

public class Example
{
   public static void Main()
   {
      String[] values = { null, "160519", "9432.0", "16,667",
                          "   -322   ", "+4302", "(100);", "01FA" };
      foreach (var value in values) {
         int number;

         bool result = Int32.TryParse(value, out number);
         if (result)
         {
            Console.WriteLine("Converted '{0}' to {1}.", value, number);         
         }
         else
         {
            Console.WriteLine("Attempted conversion of '{0}' failed.", 
                               value == null ? "<null>" : value);
         }
      }
   }
}
公共类示例
{
公共静态void Main()
{
字符串[]值={null,“160519”,“9432.0”,“16667”,
“-322”、“+4302”、“(100);”、“01FA”};
foreach(值中的var值){
整数;
bool result=Int32.TryParse(值,输出编号);
如果(结果)
{
WriteLine(“将{0}转换为{1}.”,值,数字);
}
其他的
{
WriteLine(“尝试转换“{0}”失败。”,
值==null?“:值);
}
}
}
}
输出

//       Attempted conversion of '<null>' failed.
//       Converted '160519' to 160519.
//       Attempted conversion of '9432.0' failed.
//       Attempted conversion of '16,667' failed.
//       Converted '   -322   ' to -322.
//       Converted '+4302' to 4302.
//       Attempted conversion of '(100);' failed.
//       Attempted conversion of '01FA' failed.
//尝试转换“”失败。
//将“160519”转换为160519。
//尝试转换“9432.0”失败。
//尝试转换“16667”失败。
//已将“-322”转换为-322。
//将“+4302”转换为4302。
//试图转换
//       Attempted conversion of '<null>' failed.
//       Converted '160519' to 160519.
//       Attempted conversion of '9432.0' failed.
//       Attempted conversion of '16,667' failed.
//       Converted '   -322   ' to -322.
//       Converted '+4302' to 4302.
//       Attempted conversion of '(100);' failed.
//       Attempted conversion of '01FA' failed.