我已经纠正了我之前的问题,现在能够将数据插入到表格中 现在在读取数据时,Name,Age,Gender,City,TypeofDisease条件没有得到结果..结果总是我得到“System.Data.SqlClient.SqlDataReader” 这是我的存储过程:
CREATE PROCEDURE SearchPatient
(
@Name varchar(50),
@Age int,
@Gender varchar(50),
@City varchar(50),
@TypeofDisease varchar(50)
)
AS
select * from Patient where Name=@Name and Age=@Age and Gender=@Gender and City=@City and TypeofDisease=@TypeofDisease
GO
这是我的事件代码:
protected void BtnSearch_Click(object sender, EventArgs e)
{
string name = TxtName.Text.Trim();
int age = Convert.ToInt32(TxtAge.Text);
string gender;
if (RadioButtonMale.Checked)
{
gender = RadioButtonMale.Text;
}
else
{
gender = RadioButtonFemale.Text;
}
string city = DropDownListCity.SelectedItem.Value;
string typeofdisease = "";
foreach (ListItem li in CheckBoxListDisease.Items)
{
if (li.Selected)
{
typeofdisease += li.Value;
}
}
string[] sl=typeofdisease.Split();
foreach (string s in sl)
{
ListItem itm = CheckBoxListDisease.Items.FindByValue(s);
if (itm != null)
{
itm.Selected = true;
}
}
string ConString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
SqlConnection con = new SqlConnection(ConString);
con.Open();
SqlCommand com = new SqlCommand("SearchPatient", con);
com.CommandType = CommandType.StoredProcedure;
try
{
com.Parameters.Add("@Name", SqlDbType.VarChar, 50).Value = name;
com.Parameters.Add("@Age", SqlDbType.Int).Value = age;
com.Parameters.Add("@Gender", SqlDbType.VarChar, 50).Value = gender;
com.Parameters.Add("@City", SqlDbType.VarChar, 50).Value = city;
com.Parameters.Add("@TypeofDisease", SqlDbType.VarChar, 50).Value = sl.ToString();
SqlDataReader dr;
dr = com.ExecuteReader();
StringBuilder sb = new StringBuilder();
sb.Append(dr);
lblPatientDetails.Text = sb.ToString();
}
catch
{
throw;
}
finally
{
con.Close();
com.Dispose();
}
}
请某人修改我的代码,以达到我预期的结果..
答案 0 :(得分:0)
您需要使用dr SqlDataReader中的方法。例如
你继续获得“System.Data.SqlClient.SqlDataReader”而不是某些结果的原因是,将dr分配给字符串(通过StringBuilder.Append)隐式使用此对象的ToString()方法,生成这个特定的字符串(一个有用的调试功能,但不是很适用)。
答案 1 :(得分:0)
dr = com.ExecuteReader();
StringBuilder sb = new StringBuilder();
sb.Append(dr);
lblPatientDetails.Text = sb.ToString();
更改为:
using(SqlDataReader rdr = com.ExecuteReader())
{
if(rdr.HasRows)
{
while(rdr.Read())
{
sb.Append(rdr.GetString(0));
..... etc
}
}
}
或者您可以创建DataSet。
using (SqlDataAdapter dataAdapter = new SqlDataAdapter(com))
{
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
return dataSet;
}