【问题标题】:Creating database connection class in c#在 C# 中创建数据库连接类
【发布时间】:2015-11-28 21:50:32
【问题描述】:

我是学习 C# 的初学者。我编写了这段代码,以在组合框中显示数据库中的数据,并以相同的形式连接,然后我的老师要求我为连接创建一个类并从我正在使用的任何形式调用它。

我已经在这里建立了联系。是否只是将代码删除到新类并调用?因为我在互联网上找到了很多课程为连接创建的代码与我的不同。

这段代码是在组合框中显示数据

SqlConnection con = new SqlConnection("Data Source");
SqlCommand cmd = new SqlCommand("Select ", con);

// con.Open();
SqlDataReader DR1;

try
{
    con.Open();
    DR1 = cmd.ExecuteReader();

    while (DR1.Read())
    {
        int BayN = Convert.ToInt32(DR1["BayNumber"]);
        comboBox1.Items.Add(BayN);
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
finally
{
    con.Close();
}

另一个连接是在文本框中显示 8 行数据:

SqlConnection con = new SqlConnection("Data Source");
DataSet dsa = new DataSet();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("Select ='" + comboBox1.Text.Trim() + "';", con);
da.Fill(dsa);

for (int i = 0; i <= 8; i++)
{
    for (int k = 0; k <= 8; k++)
    {
        textBox1.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
        label2.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
        textBox2.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
        label4.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
        textBox3.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
        label6.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
        textBox4.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
        label8.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
        textBox5.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
        label10.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
        textBox6.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
        label12.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
        textBox7.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
        label14.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
        textBox8.Text = dsa.Tables[0].Rows[i++]["PatientId"].ToString();
        label16.Text = dsa.Tables[0].Rows[k++]["Status"].ToString();
    }
}

如果我说得太深或太详细,我很抱歉,但要说清楚(: 我现在迷路了,因为我正在使用DataSetDataReader 可以从另一个班级调用它们吗?

【问题讨论】:

  • SqlCommand cmd = new SqlCommand("Select ", con); 实际的Select Statement在哪里
  • 哇,这么快^_^,我只是把它缩短
  • 对于初学者,您需要了解类是如何工作的,您需要了解如何返回 DataSet、DataTable 等...我将发布一些您可以放入自己的 SqlHelpers 类中的内容
  • 非常感谢我真的很感激,我在等你(:
  • for (int i = 0; i &lt;= 8; i++) { for (int k = 0; k &lt;= 8; k++) { 不必要的代码,您需要了解如何迭代填充 DataTable 或 DataSet 的 DataTable .. 填充 DataTable 比返回 DataSet 更容易,除非您返回 DataSet.Tables[0]

标签: c# sql-server database visual-studio class


【解决方案1】:
public static class SqlDBHelper
{
    public static DataSet ExecuteDataSet(string sql, CommandType cmdType, params SqlParameter[] parameters)
    {
        using (DataSet ds = new DataSet())
        using (SqlConnection connStr = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConn"].ConnectionString))
        using (SqlCommand cmd = new SqlCommand(sql, connStr))
        {
            cmd.CommandType = cmdType;
            foreach (var item in parameters)
            {
                cmd.Parameters.Add(item);
            }

            try
            {
                cmd.Connection.Open();
                new SqlDataAdapter(cmd).Fill(ds);
            }
            catch (SqlException ex)
            {
                //log to a file or Throw a message ex.Message;
            }
            return ds;
        }
    }

    public static DataTable ExecuteDataSet(string sql, CommandType cmdType, params SqlParameter[] parameters)
    {
        using (DataSet ds = new DataSet())
        using (SqlConnection connStr = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConn"].ConnectionString))
        using (SqlCommand cmd = new SqlCommand(sql, connStr))
        {
            cmd.CommandType = cmdType;
            foreach (var item in parameters)
            {
                cmd.Parameters.Add(item);
            }

            try
            {
                cmd.Connection.Open();
                new SqlDataAdapter(cmd).Fill(ds);
            }
            catch (SqlException ex)
            {
                //Show a message or log a message on ex.Message
            }
            return ds.Tables[0];
        }
    }   
}

如果您想在类中添加另一个返回 DataTable 的方法,请执行以下操作

【讨论】:

    猜你喜欢
    • 2012-04-11
    • 1970-01-01
    • 2010-12-14
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多