winform窗体中,随机抽取数据库表中的问题。假设有三个科目的问题,当选中某个科目时,自动抽取对应的值
1个回答
展开全部
不知道你的数据库是那种 假如是access的吧
string subjectName=Combox.Tag.ToString(); //// combox的tag属性存储数据库科目对应的表名
string strConnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + @"你的数据库路径";
OleDbConnection oleDBcon = new OleDbConnection(strConnection);
oleDBcon.Open();
DataTable dt = new DataTable();
OleDbDataAdapter odbAdapter = null;
string strSQL = string.Format("select * from {0}", subjectName);
if (oleDBcon.State == System.Data.ConnectionState.Closed)
{
oleDBcon.Open();
}
try
{
string sDatetime = System.DateTime.Now.ToString("mmss");
OleDbCommand oleDBcmd = new OleDbCommand(strSQL, oleDBcon);
odbAdapter = new OleDbDataAdapter(oleDBcmd);
odbAdapter.Fill(dt); //// 获取对应的表
DataRow pRow=null;
for (int i = 0; i < dt.Rows.Count; i++)
{
pRow = dt.Rows[i]; //// 第i道题目
}
}
string subjectName=Combox.Tag.ToString(); //// combox的tag属性存储数据库科目对应的表名
string strConnection = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + @"你的数据库路径";
OleDbConnection oleDBcon = new OleDbConnection(strConnection);
oleDBcon.Open();
DataTable dt = new DataTable();
OleDbDataAdapter odbAdapter = null;
string strSQL = string.Format("select * from {0}", subjectName);
if (oleDBcon.State == System.Data.ConnectionState.Closed)
{
oleDBcon.Open();
}
try
{
string sDatetime = System.DateTime.Now.ToString("mmss");
OleDbCommand oleDBcmd = new OleDbCommand(strSQL, oleDBcon);
odbAdapter = new OleDbDataAdapter(oleDBcmd);
odbAdapter.Fill(dt); //// 获取对应的表
DataRow pRow=null;
for (int i = 0; i < dt.Rows.Count; i++)
{
pRow = dt.Rows[i]; //// 第i道题目
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询