1.首先获取datagirdview的datatable
DataTable dt = dataGridView1.DataSource as DataTable;
2.将其更新至数据库
public static bool UpdataSql(DataTable dt) { try { using (SqlConnection connection = new SqlConnection(PubVariant.ConnectionString)) { string strSql = "select * from CorrespondField"; using (SqlCommand cmd = new SqlCommand(strSql, connection)) { using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { using (SqlCommandBuilder builder = new SqlCommandBuilder(da)) { da.Update(dt); return true; } } } } } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } }
连接信息字段如下:
public class PubVariant { public static string connectionString = "server=localhost;database=Test;user id=sa;password=123456"; public static string ConnectionString { get { return connectionString; } set { connectionString = value; } } }
转载:http://blog.csdn.net/foreverling/article/details/37376675
时间: 2024-09-08 11:13:35