0
我已经完成了一个包含gridview
的c#
表单。我想将选定的行数据插入按钮单击的临时表。我已经尝试过这样但它不起作用。它显示了错误:如何创建一个sql临时表并使用sql命令向其中插入数据(C#)
下面String or Binary Data would be truncated. The statement has been terminated
是我的代码: -
private void button1_Click(object sender, EventArgs e)
{
int i = 0;
List<int> ChkedRow = new List<int>();
for (i = 0; i <= dataGridView1.RowCount - 1; i++)
{
if (Convert.ToBoolean(dataGridView1.Rows[i].Cells["Column1"].Value) == true)
{
ChkedRow.Add(i);
}
}
if (ChkedRow.Count == 0)
{
MessageBox.Show("Select Items to view report");
return;
}
foreach (int j in ChkedRow)
{
String ConnectionString1 = @"Data Source=.\SQLEXPRESS;Initial Catalog=tempdb;Integrated Security=True;Pooling=False";
cnnStr = @"create table ##gridtemp (itmcod varchar(7)) INSERT INTO ##gridtemp (itmcod)
VALUES ('" + dataGridView1.Rows[j].Cells["title"].Value.ToString() + "');";
try
{
using (SqlConnection cs = new SqlConnection(ConnectionString1))
{
using (cmd = new SqlCommand(cnnStr, cs))
{
cs.Open();
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
MessageBox.Show("Records Added Succesfully");
}
你为什么要在一个临时表中插入? – Shaharyar
主表中包含大量的记录。我只需要过滤的记录报告 –
错误描述非常清楚:'dataGridView1.Rows [j] .Cells [“title”]。Value.ToString()'是一个大于7个字符的字符串。 –