private DataSet ds = new DataSet();
DataTable dt = new DataTable();
private System.Data.SqlClient.SqlDataAdapter dap = new System.Data.SqlClient.SqlDataAdapter();
BindingSource bs = new BindingSource();
private void CoalVerifiedHole_Dialog_Load(object sender, EventArgs e)
{
BindDataToDataGridView();
}
//private string strConnection = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=E:\毕业设计\GraduationDesign\GraduationDesign\DBAccess\BaiPingCoalMine.mdb";
public DataSet AccessDBData(string sqlString, string tableName)
{
try
{
using (System.Data.SqlClient.SqlConnection sqlconn = new System.Data.SqlClient.SqlConnection("Data Source=PC-20140711CKCI\\SQLEXPRESS;Initial Catalog=白坪矿;Integrated Security=True"))
{
using (System.Data.SqlClient.SqlCommand SqlCmd = new System.Data.SqlClient.SqlCommand())
{
SqlCmd.Connection = sqlconn;
SqlCmd.CommandText = sqlString;
dap.SelectCommand = SqlCmd;
sqlconn.Open();
dap.Fill(ds);
}
sqlconn.Close();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + ex.StackTrace);
}
return ds;
}
/// <summary>
/// 从数据库获取数据
/// </summary>
/// <returns></returns>
private void AccessData()
{
string selectsql = string.Format("select [ID],[UnitID],[LoopID],[HoleNum],[Name],[X],[Y],[Z],[Azimuth],[DipAngle],[SideDistance],[BottomDistance],[SampleDepth],[LengthCoal],[LengthRock],[StartDate],[EndDate],[EmissionDate],[GasPressure],[GasContent],[SpotMostParsing],[StopReason],[CH4Content],[ExceptionDescription],[SampleStuff],[AnalystStuff],[ReportNum] from {0}", "dbo.CoalVerifiedHole");
AccessDBData(selectsql, "dbo.CoalVerifiedHole");
}
/// <summary>
/// 绑定数据到DataGridView
/// </summary>
private void BindDataToDataGridView()
{
AccessData();
bs.DataSource = ds.Tables[0];
dt = ds.Tables[0];
bindingNavigator1.BindingSource = bs;
dgv_CoalVerifiedHole_Dialog.DataSource = bs;
dgv_CoalVerifiedHole_Dialog.Columns["ID"].Visible = false;
}
private void btnSave_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("确定要保存吗?", "提示!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
if (dap != null)
{
System.Data.SqlClient.SqlCommandBuilder Sqlcb = new System.Data.SqlClient.SqlCommandBuilder(dap as System.Data.SqlClient.SqlDataAdapter);
dap.DeleteCommand = Sqlcb.GetDeleteCommand();
dap.InsertCommand = Sqlcb.GetInsertCommand();
dap.UpdateCommand = Sqlcb.GetUpdateCommand();
dap.Update(dt);
MessageBox.Show("保存成功!", "提示");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
大神帮我看下,困惑好几天了。都不知道怎么办。。。。。。。。。。。。。。。。。。。