作者fr730149 ()
看板C_Sharp
標題[問題] SqlDataReader讀不到值…
時間Wed May 25 20:18:12 2011
語法如下:
public int Select_Mid(int Tid)
{
int result;
string SqlString = "";
SqlString = "Select Mid From Article Where Tid = @Tid";
SqlConnection Sql_Conn = new SqlConnection(Sql_ConnString);
SqlCommand Sql_Command = new SqlCommand();
Sql_Command.Connection = Sql_Conn;
Sql_Command.CommandText = SqlString;
Sql_Command.Parameters.AddWithValue("@Tid", Tid);
using (Sql_Conn)
{
Sql_Conn.Open();
using (SqlDataReader dr = Sql_Command.ExecuteReader())
{
if (dr.Read())
result = (int)dr["Mid"];
}
}
return result;
}
Compiler 一直提示我 「result 使用未指定的區域變數」
請問:上方的語法那裡有錯?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 116.118.161.39
1F:→ mathlinka:int result = new int(); 試試? 05/25 20:47
2F:推 hatasum:int result; 改成int result=0; 這就是compiler提示你的 05/25 20:48
3F:→ fr730149:樓上兩位皆為正解,感謝 05/25 21:12