C#中怎么将sqldatareader类型转换成string类型,求高手帮忙!!
protectedvoidButton1_Click(objectsender,EventArgse){stringzhanghao=TextBox1.Text.Trim...
protected void Button1_Click(object sender, EventArgs e)
{
string zhanghao = TextBox1.Text.Trim();
string mima = TextBox2.Text.Trim();
string lianjie = " Server = localhost; database = zhaoyanjing_keyan不要丢;Integrated Security=true";
SqlConnection conn = new SqlConnection(lianjie);
conn.Open();
SqlCommand comm = new SqlCommand("select 密码 from 账号和密码 where 账号='" + zhanghao + "'", conn);
SqlDataReader myreader = comm.ExecuteReader();
try
{
string reader = myreader.GetString(1);
if (mima==reader.Trim())
Response.Redirect("个人主页.aspx");
else Response.Write("账号或密码错误!请重新输入!");
myreader.Close();
conn.Close();
}
catch (Exception a) { Response.Write(a); }
} 展开
{
string zhanghao = TextBox1.Text.Trim();
string mima = TextBox2.Text.Trim();
string lianjie = " Server = localhost; database = zhaoyanjing_keyan不要丢;Integrated Security=true";
SqlConnection conn = new SqlConnection(lianjie);
conn.Open();
SqlCommand comm = new SqlCommand("select 密码 from 账号和密码 where 账号='" + zhanghao + "'", conn);
SqlDataReader myreader = comm.ExecuteReader();
try
{
string reader = myreader.GetString(1);
if (mima==reader.Trim())
Response.Redirect("个人主页.aspx");
else Response.Write("账号或密码错误!请重新输入!");
myreader.Close();
conn.Close();
}
catch (Exception a) { Response.Write(a); }
} 展开
2个回答
展开全部
string reader = myreader.GetString(1);取值前加个if判断
if(myReader.Read()){//表示读取到值了
string reader = myreader.GetString(1);
}
你直接取值可能会报错的,另外用myreader.GetString(1)不行的话换成
myreader.Reader[1].toString()试试
最后注意取值时索引是从0开始的,也既是取查询到的第一列的值的话,括号里面的就是0了,纯手打,楼主若还有什么问题,请追问
if(myReader.Read()){//表示读取到值了
string reader = myreader.GetString(1);
}
你直接取值可能会报错的,另外用myreader.GetString(1)不行的话换成
myreader.Reader[1].toString()试试
最后注意取值时索引是从0开始的,也既是取查询到的第一列的值的话,括号里面的就是0了,纯手打,楼主若还有什么问题,请追问
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询