本文介绍了无法将数据从datagridview插入数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
无法将数据从datagridview插入到数据库表中。
任何人都可以帮助解决这个问题。
Hi,
Unable to insert data from datagridview into database table .
can anyone help in solving this .
private void Form1_Load(object sender, EventArgs e)
{
ServiceController[] services = ServiceController.GetServices();
dataGridView1.DataSource = services ;
}
public SqlConnection getsqlcon()
{
string conn = "Data Source=HostName;Initial Catalog=MSNETDB;Integrated Security=True;Pooling=False";
SqlConnection mycon=new SqlConnection (conn);
return mycon;
}
public bool getcomm(string connstring1)
{
SqlConnection sqlcon = this.getsqlcon();
SqlCommand com = new SqlCommand(connstring1, sqlcon);
sqlcon.Open();
com.ExecuteNonQuery();
com.Dispose();
sqlcon.Close();
sqlcon.Dispose();
return true;
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = Helper.ConnectionString;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
try
{
string col1 = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
string col2 = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString();
string col3 = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString();
string col4 = dataGridView1[6, dataGridView1.CurrentCell.RowIndex].Value.ToString();
string insert_sql="insert into Services(DisplayName,ServiceName,Status,ServiceType)values('"+col1 +"','"+col2 +"','"+col3 +"','"+col4+ "')";
if (this.getcomm(insert_sql))
{
MessageBox.Show("Insertion Success");
}
else
{
MessageBox.Show("Insertion Failed");
}
}
catch
{
}
感谢问候
sam.198979
Thanks Regards
sam.198979
推荐答案
这篇关于无法将数据从datagridview插入数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!