我正在使用C#(asp.net)。我有两个表(数据和细节)在同一个数据库中。从两个不同表中提取数据库中的数据
表 “数据”
id | chap | unit |
----------------
1| chap1|unit1 |
2| chap2|unit2 |
3| chap3|unit3 |
表 “方式”
id| code| num |
----------------
1|abc |2 |
2|efg |3 |
3|hij |1 |
现在我想取从 “NUM”,其中代码= “EFG”(在表 “方式”)的值。并使用相同的值(3)通过id从表“data”中获取数据。我正在使用此代码。
OleDbConnection conn = new OleDbConnection(*** ...... *****);
OleDbCommand cmd;
OleDbDataReader reader;
String query = String.Format("select num from details where code="efg");
cmd = new OleDbCommand(query, conn);
reader = cmd.ExecuteReader();
int num = int.Parse(reader.GetValue(0).ToString());
query = String.Format("select chap from data where id={0}",num);
cmd = new OleDbCommand("select lesson from data where id=3", conn);
reader = cmd.ExecuteReader();
Label1.Text = reader.GetValue(0).ToString();
但它显示错误。它显示“没有数据存在行/列”。
其实I M使用微软的数据库访问,这就是为什么我使用“的OleDbConnection”。对不起,我错误地标记了SQL Server。 – Sagar 2012-04-11 16:35:48
@Viktor:我的答案解决了你的问题吗?如果是的话,你应该[接受它](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work),如果你认为它很好。 – Marco 2012-04-12 05:52:16