我已暂停包含c#
的{{1}}表单。我想在按钮单击时将所选行数据插入临时表。我试过这样但不起作用。它显示错误:
字符串或二进制数据将被截断。该声明已被终止
下面是我的代码: -
gridview
答案 0 :(得分:0)
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn1 = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=tempdb;Integrated Security=True");
SqlCommand cmd1 = new SqlCommand("create table ##gridtemp (itmcod varchar(MAX))", conn1);
conn1.Open();
cmd1.ExecuteNonQuery();
DataTable dts = new DataTable();
SqlBulkCopy bulkCopy = new SqlBulkCopy(conn1);
bulkCopy.DestinationTableName = "##gridtemp";
bulkCopy.WriteToServer(dts);
conn1.Close();
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 = @"INSERT INTO ##gridtemp (itmcod)
VALUES ('" + dataGridView1.Rows[j].Cells["itmcod"].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");
}
}