private void btnsave_Click(object sender, EventArgs e)
{
try
{
for (int i = 0; i < itemgrid.RowCount - 1; i++ )
{
dru.insertdata("insert into tbl_godown (date,category,product,quantity,MRP,salesrate,margin,Total,vendor,unit)values('" + itemgrid.Rows[i].Cells["Column1"].Value.ToString() + "','" + itemgrid.Rows[i].Cells["Column2"].Value.ToString() + "','" + itemgrid.Rows[i].Cells["Column3"].Value.ToString() + "','" + itemgrid.Rows[i].Cells["Column4"].Value.ToString() + "','" + double.Parse(itemgrid.Rows[i].Cells["Column5"].Value.ToString()) + "','" + Convert.ToDouble(itemgrid.Rows[i].Cells["Column6"].Value.ToString()) + "','" + Convert.ToDouble(itemgrid.Rows[i].Cells["Column7"].Value.ToString()) + "','" + Convert.ToDouble(itemgrid.Rows[i].Cells["Column8"].Value.ToString()) + "','" + Convert.ToDouble(itemgrid.Rows[i].Cells["Column9"].Value.ToString()) + "','" + Convert.ToDouble(itemgrid.Rows[i].Cells["Column10"].Value.ToString()) + "') ");
MessageBox.Show("Insert Successfully");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
将数据从datagridview插入数据库,datagridview的名称是itemgrid
private void txtquantity_TextChanged(object sender, EventArgs e)
{
mulfunc();
}
private void txtrate_TextChanged(object sender, EventArgs e)
{
mulfunc();
marginfunc();
}
private void txtMRP_TextChanged(object sender, EventArgs e)
{
marginfunc();
}
/// <summary>
/// margin calculations(sales rate-vendor rate = margin)
/// </summary>
public void marginfunc()
{
try
{
double val1;
double val2;
if (!double.TryParse(txtMRP.Text, out val1) || !double.TryParse(txtrate.Text, out val2))
return;
double val3 = val2 - val1;
// Here you define what TextBox should show the multiplication result
txtmargin.Text = val3.ToString();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// quantity * sales rate
/// </summary>
public void mulfunc()
{
try
{
double val1;
double val2;
if (!double.TryParse(txtquantity.Text, out val1) || !double.TryParse(txtrate.Text, out val2))
return;
double val3 = val1 * val2;
// Here you define what TextBox should show the multiplication result
txttotal.Text = val3.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void clear()
{
cmbunit.Text = "";
txtprod.Text = "";
txtquantity.Text = "";
txtMRP.Text = "";
txtrate.Text = "";
txtmargin.Text = "";
txttotal.Text = "";
txtvendor.Text = "";
}
答案 0 :(得分:1)
首先,我不建议构建动态SQL以这种方式执行INSERT,因为这会带来SQL注入风险(https://en.wikipedia.org/wiki/SQL_injection)。但如果您的代码是非生产的(即您不关心上述风险)请尝试使用此模式......
{
for (int i = 0; i < itemgrid.RowCount - 1; i++ )
{
var sInsertSQL = string.Format(@"
insert tbl_godown (
date,category,product,quantity,MRP,salesrate,margin,Total,vendor,unit
)
values(
'{0}','{1}','{2}','{3}','{4}',{5},{6},{7},{8},{9}
)'",
itemgrid.Rows[i].Cells["Column1"].Value,
itemgrid.Rows[i].Cells["Column2"].Value,
itemgrid.Rows[i].Cells["Column3"].Value,
itemgrid.Rows[i].Cells["Column4"].Value,
Convert.ToDouble(itemgrid.Rows[i].Cells["Column5"].Value),
Convert.ToDouble(itemgrid.Rows[i].Cells["Column6"].Value),
Convert.ToDouble(itemgrid.Rows[i].Cells["Column7"].Value),
Convert.ToDouble(itemgrid.Rows[i].Cells["Column8"].Value),
Convert.ToDouble(itemgrid.Rows[i].Cells["Column9"].Value),
Convert.ToDouble(itemgrid.Rows[i].Cells["Column10"].Value)
);
dru.insertdata(sInsertSQL);
MessageBox.Show("Insert Successfully");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
这是一种更容易阅读/调试的模式。您始终可以将sInsertSQL输出到直接窗格,然后复制/粘贴到SSMS中以找出错误。