Sophie1_ 2015-04-19 11:52 采纳率: 0%
浏览 1824

DataAdapter 更新数据总是提示ConnetionString属性尚未初始化

    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);
        }
    }



            大神帮我看下,困惑好几天了。都不知道怎么办。。。。。。。。。。。。。。。。。。。
  • 写回答

3条回答 默认 最新

  • Sophie1_ 2015-04-19 11:54
    关注

    能显示数据 ,但就是更新时会出现问题。

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog