【发布时间】:2018-01-20 09:04:32
【问题描述】:
起初我在将数据插入数据库时出错。错误是:
“输入的字符串格式不正确”
但后来我使用Int.TryParse 将string 转换为int,并且成功了。但我现在面临的问题是 Int.TryParse 只将 1 或 0 之类的布尔值传递给数据库。
例如:如果我在textbox 中写入 34,然后单击确定插入。 textbox 只是将布尔值传递给数据库。 IE; 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();
}
}
【问题讨论】:
-
Int32.TryParse返回一个布尔值,因此结果是可以预期的。如果您想保存解析结果,请查看您未使用的时髦out参数。然后阅读How to Ask 并采取tour -
你应该阅读关于TryParse的文档
-
我认为他/她对 out 参数的使用比其他任何事情都感到困惑。
标签: c# winforms visual-studio int tryparse