最近做一个页面遇到从一个text框中一次获取多值参数的问题(如:一次可输入多个学生ID),点击查看后以表格形式返回查询结果!其中查询语句关联了两张表(),
Select *From dbo.stu Inner Join dbo.stuScore on stu.Id=stuScore.Id Where stu.Id IN (@Id)) Order by stuScore.Score
我在公共类里这样建立方法:
private static readonly string sqlcommandstr = @"Select *From dbo.stu Inner Join dbo.stuScore on stu.Id=stuScore.Id Where stu.Id IN (@Id)) Order by stuScore.Score"; public static DataTable GetDateTable(string Id)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ToString()))
{
using (SqlCommand command = new SqlCommand(sqlcommandstr, connection))
{
command.Parameters.AddWithValue("@Id",Id); connection.Open(); SqlDataAdapter sda = new SqlDataAdapter(sqlcommandstr, connection); DataTable dt = new DataTable(); sda.Fill(dt); return dt; connection.Close();
}
} }
该页面中的相关代码我是这样写的:private string stuIdsParameter
{
get
{
string stuIds = String.Empty; int output, stuCount = 0; StringReader reader = new StringReader(stuIdTextBox.Text); string line = reader.ReadLine(); string nextline = reader.ReadLine(); while (nextline != null)
{
if (Int32.TryParse(nextline, out output))
{
if (Int32.TryParse(line, out output))
{
stuIds += String.Format("{0},", line); srCount++;
}
line = nextline;
}
nextline = reader.ReadLine();
}
if (Int32.TryParse(line, out output))
{
stuIds += line; srCount++;
}
return stuIds;
}
}
protected void viewButton_Click(object sender, ClickEventArgs e)
{
string StuId = this.stuIdsParameter; DataTable dt = DataAccess.GetDateTable(StuId); CasesGridView.DataSource = dt; CasesGridView.DataBind();
}
执行结果显示参数声明有错,有哪位高手帮忙看看!小弟不胜感激!
Select *From dbo.stu Inner Join dbo.stuScore on stu.Id=stuScore.Id Where stu.Id IN (@Id)) Order by stuScore.Score
我在公共类里这样建立方法:
private static readonly string sqlcommandstr = @"Select *From dbo.stu Inner Join dbo.stuScore on stu.Id=stuScore.Id Where stu.Id IN (@Id)) Order by stuScore.Score"; public static DataTable GetDateTable(string Id)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ToString()))
{
using (SqlCommand command = new SqlCommand(sqlcommandstr, connection))
{
command.Parameters.AddWithValue("@Id",Id); connection.Open(); SqlDataAdapter sda = new SqlDataAdapter(sqlcommandstr, connection); DataTable dt = new DataTable(); sda.Fill(dt); return dt; connection.Close();
}
} }
该页面中的相关代码我是这样写的:private string stuIdsParameter
{
get
{
string stuIds = String.Empty; int output, stuCount = 0; StringReader reader = new StringReader(stuIdTextBox.Text); string line = reader.ReadLine(); string nextline = reader.ReadLine(); while (nextline != null)
{
if (Int32.TryParse(nextline, out output))
{
if (Int32.TryParse(line, out output))
{
stuIds += String.Format("{0},", line); srCount++;
}
line = nextline;
}
nextline = reader.ReadLine();
}
if (Int32.TryParse(line, out output))
{
stuIds += line; srCount++;
}
return stuIds;
}
}
protected void viewButton_Click(object sender, ClickEventArgs e)
{
string StuId = this.stuIdsParameter; DataTable dt = DataAccess.GetDateTable(StuId); CasesGridView.DataSource = dt; CasesGridView.DataBind();
}
执行结果显示参数声明有错,有哪位高手帮忙看看!小弟不胜感激!
解决方案 »
- WPF,标记扩展学不懂
- c#怎么编写dll
- 在winform编程中哪个数据库控件带有分页功能啊
- 在c#的制作桌面登录程序中,怎么实现登录名只能输入半角英文字母,密码只能输入半角英文字母和半角英文数字
- 我是新手,谁能详细的告诉我此段代码的执行顺序,非常感谢!
- 动态绑定datagrid,刷新后页面之后不能显示该列。
- 怎么知道数据类型是几位小数
- 用代码实现当DataGridView行数改变时,DataGridView第1列为第2列列长一半,第3列列长为第2列列长的2倍,怎么搞?
- SqlDataReader中如何返回行数?
- 有没有办法在vc6中调用vc7(c#)开发的接口啊?
- 点保存 出现错误提示,保存到数据库中失败。
- 请问迅雷里那个列表是怎么做的啊
都是ID int 类型的话 在组合的时候判断一下就可以了 照样防注入