我是学习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();
}
}
如果我走得太深或太详细,我很抱歉,但要说清楚(:
我现在迷路了,因为我使用DataSet
和DataReader
可以从另一个类调用它们吗?
答案 0 :(得分:0)
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的方法,请执行以下操作