有很多问题与我的问题类似,但没有一个问题能帮助我解决这个问题 我正在使用win-forms,使用SQL服务器
在C#中编码我有一个datagridview,它从数据库中检索数据,一些数据作为文本,一些是图像
我的代码如下所示,将datagridview数据保存到数据库中
但这不起作用并抛出错误对象或列名称丢失或为空。对于select into语句,验证..........
请解决我的问题..
try
{
ConnectionStringSettings consettings = ConfigurationManager.ConnectionStrings["attendancemanagement"];
string connectionString = consettings.ConnectionString;
string StrQuery;
try
{
using (SqlConnection conn = new SqlConnection(connectionString))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
conn.Open();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
StrQuery = @"INSERT INTO dailydemo VALUES ("
+ dataGridView1.Rows[i].Cells["S_No"].Value + ", "
+ dataGridView1.Rows[i].Cells["Employee_Id"].Value + ", "
+ dataGridView1.Rows[i].Cells["Employee_Name"].Value + ", "
+ dataGridView1.Rows[i].Cells["In_time"].Value + ", "
+ dataGridView1.Rows[i].Cells["Out_Time"].Value + ", "
+ dataGridView1.Rows[i].Cells["Date"].Value + ", "
+ dataGridView1.Rows[i].Cells["Week_No_of_The_Month"].Value + ", "
+ dataGridView1.Rows[i].Cells["Attendance"].Value + ", "
+ dataGridView1.Rows[i].Cells["Work_status"].Value + ", "
+ dataGridView1.Rows[i].Cells["Remarks"].Value + ", "
+ dataGridView1.Rows[i].Cells["Image_of_Employee"].Value + ");";
comm.CommandText = StrQuery;
comm.ExecuteNonQuery();
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
答案 0 :(得分:0)
使用此功能可能会全部帮助...
byte[] imgByteArrLogo = new byte[0];
using (SqlConnection con = new SqlConnection("Your Connection string Here"))
{
if (con.State == ConnectionState.Closed)
{
con.Open();
}
using (SqlTransaction _pTrans = con.BeginTransaction(IsolationLevel.ReadCommitted))
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (dataGridView1.Rows[i].Cells["Image_of_Employee"].Value != null)
{
Image _dgvImage = (Image)dataGridView1.Rows[i].Cells["Image_of_Employee"].Value;
MemoryStream ms = new MemoryStream();
_dgvImage.Save(ms, ImageFormat.Jpeg);
imgByteArrLogo = new byte[ms.Length];
ms.Position = 0;
ms.Read(imgByteArrLogo, 0, imgByteArrLogo.Length);
}
else
{
imgByteArrLogo = new byte[0];
}
try
{
string sql = "INSERT INTO dailydemo (S_No,Employee_Id ,Employee_Name ,In_time ,Out_Time ,Date,Week_No_of_The_Month,Attendance,Work_status,Remarks,Image_of_Employee) VALUES (@S_No,@Employee_Id ,@Employee_Name ,@In_time ,@Out_Time ,@Date,@Week_No_of_The_Month,@Attendance,@Work_status,@Remarks,@Image_of_Employee)";
using (SqlCommand cmd = new SqlCommand(sql, con, _pTrans))
{
cmd.Parameters.Add(new SqlParameter("@S_No", dataGridView1.Rows[i].Cells["S_No"].Value));
cmd.Parameters.Add(new SqlParameter("@Employee_Id", dataGridView1.Rows[i].Cells["Employee_Id"].Value));
cmd.Parameters.Add(new SqlParameter("@Employee_Name", dataGridView1.Rows[i].Cells["Employee_Name"].Value));
cmd.Parameters.Add(new SqlParameter("@In_time", dataGridView1.Rows[i].Cells["In_time"].Value));
cmd.Parameters.Add(new SqlParameter("@Out_Time", dataGridView1.Rows[i].Cells["Out_Time"].Value));
cmd.Parameters.Add(new SqlParameter("@Week_No_of_The_Month", dataGridView1.Rows[i].Cells["Week_No_of_The_Month"].Value));
cmd.Parameters.Add(new SqlParameter("@Attendance", dataGridView1.Rows[i].Cells["Attendance"].Value));
cmd.Parameters.Add(new SqlParameter("@Work_status", dataGridView1.Rows[i].Cells["Work_status"].Value));
cmd.Parameters.Add(new SqlParameter("@Remarks", dataGridView1.Rows[i].Cells["Remarks"].Value));
cmd.Parameters.Add(new SqlParameter("@Image_of_Employee", imgByteArrLogo));
cmd.ExecuteScalar();
}
}
catch (Exception exp)
{
_pTrans.Rollback();
con.Close();
_pTrans.Dispose();
MessageBox.Show(exp.Message, "Error!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
_pTrans.Commit();
_pTrans.Dispose();
con.Close();
}
}
更新:
请注意,在此示例中,我使用了datagridview单元格名称作为sql表列名称,如果列名不同,则修改它。