大家好,我用C#写连接SQLSERVER的代码,虽然能成功运行,但感觉不怎么完整,比如是否要手动关闭连接,是否要写异常处理等等。麻烦大家帮我看看代码,给我完整的建议可以吗?
实现功能是将数据库的内容显示在GridView控件中,程序能运行成功,就是担心代码不完整。代码如下:SqlConnection conn = new SqlConnection("server=localhost;database=BOE;user=sa;password=640407");//这里是否要用open()方法呢,为什么我不用也可以运行呢?SqlCommand cmd = new SqlCommand("select SupplierName,Contact,Mobile from Supplier", conn);SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;da.Fill(ds, "dbo.Supplier");this.dataGridView1.DataSource = ds.Tables[0].DefaultView;

//是否要手动关闭连接呢?
//异常处理如何写呢?

解决方案 »

  1.   

    网上搜索SqlHelper 类,放进项目中直接使用即可。 自己写起来太麻烦了。
      

  2.   

    SqlDataAdapter 不需要手动打开和关闭连接,他会自动打开和关闭的
    异常处理可以用try  catch
      

  3.   

    最好写open和close
    如果是大型项目的话,链接不关闭会占用内存,等待垃圾回收,会影响性能
      

  4.   

    关闭的话肯定是关闭数据库连接,异常处理,加个try  catch 就行了
      

  5.   

     public DataSet getDataSet(string sql)
            {
               
                OracleConnection conn = new OracleConnection(ConnectionString);
                OracleCommand cmd = new OracleCommand(sql,conn);
                cmd.CommandType = CommandType.Text;
                DataSet ds = new DataSet();
                try
                {
                    conn.Open();
                    OracleDataAdapter ad = new OracleDataAdapter();
                    ad.SelectCommand = cmd;
                    ad.Fill(ds);
                }
                catch (Exception ex)
                {
                    strError = "数据查询失败:" + ex.Message;
                    return null;
                }
                finally
                {
                    conn.Close();
                }
                return ds;
            }
    这是一个例子,包括异常判断
      

  6.   

    string SqlStr = WebConfigurationManager.ConnectionStrings[""].ConnectionString;
                using (SqlConnection conn = new SqlConnection(SqlStr))
                {
                    conn.Open();
                    SqlCommand command = new SqlCommand();
                    command.Connection = conn;
                    command.CommandText = "select * from...";
                    command.CommandType = System.Data.CommandType.Text;
                    SqlDataAdapter sda = new SqlDataAdapter();
                    sda.SelectCommand = command;
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                }或者可以用SqlConnectionStringBuilder
    SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
                scsb.DataSource = "数据源";
                scsb.InitialCatalog = "初始目录";
                scsb.IntegratedSecurity = true;
                scsb.UserID = "";
                scsb.Password = "";
                string SqlStr = scsb.ConnectionString;
                using (SqlConnection conn = new SqlConnection(SqlStr))
                {
                    conn.Open();
                    SqlCommand command = new SqlCommand();
                    command.Connection = conn;
                    command.CommandText = "select * from...";
                    command.CommandType = System.Data.CommandType.Text;
                    SqlDataAdapter sda = new SqlDataAdapter();
                    sda.SelectCommand = command;
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                }
      

  7.   

    用using可以不写close或者dispose
      

  8.   

    SqlDataAdapter 不需要手动打开和关闭连接,他会自动打开和关闭的
    异常处理可以用try  catch 
    访问数据库的一帮都要加上异常处理防止程序崩溃。新手建议,高手勿喷。
      

  9.   

    不想以后被骂就加上open和close..
      

  10.   

    ADO.NET是这么介绍的,如果加上open就必须close,只是觉得添足.