【发布时间】:2014-07-11 18:52:10
【问题描述】:
我尝试将只有一个字段(该字段的数据是主键)的数据表转换为 int ,以便在 SQL 命令中使用 Select 等。 但它失败了! 当我将它转换为对象或先将其转换为字符串时,命令出错了! 请帮帮我
我想从具有外键的表中选择 *,其中外代码等于 int 值,该值已从另一个 sql 命令中的表中选择并作为只有一个字段的数据表行返回。
这是我的代码:
类我的数据:
public string strsql;
public DataTable showData()
{
SqlConnection Con1 = new SqlConnection("Data Source=.;database=daneshgah;integrated security=true");
Con1.Open();
SqlDataAdapter da = new SqlDataAdapter(strsql, Con1);
DataTable dt = new DataTable();
da.Fill(dt);
Con1.Close();
return (dt);
}
按钮事件:
myData search = new myData();
int aa = int.Parse(txt_stdcourse.Text);
search.strsql = "select tchNo from University where couNo='" + aa + "'";
DataTable a = search.showData();
string b = a.Rows[0][0].ToString();
int c = int.Parse(b);
myData akhz = new myData();
akhz.strsql = "insert into stc (couNo,tchNo,stuNo)values('" + aa + "','" + c + "','" + id + "')";
akhz.Data();
lbl_stdcourseok.Visible = false;
lbl_stdcourseok.Visible = true;
【问题讨论】:
-
您的代码似乎有问题。但是,除非我们有 code or information that can reproduce the problem,否则我们无能为力。否则,我们只是在盲目猜测。还请提供有关您遇到的错误的信息。
-
将一些代码与您尝试过的问题联系起来。此外,您最终是如何在数据表中获得单个值的,可能是您正在使用
DataAdapter通过数据库加载它。考虑使用ExecuteScalar,它会返回一个项目。 -
在您的数据库中更改它。
-
我猜(因为您没有提供代码)您正在尝试将一行或一组行 - 或者可能将整个表转换为 int,请提供代码,或者更好- 使用调试器准确查看您尝试将什么值转换为 int
-
你在使用SqlParameter吗?
标签: c# sql datatable command int