C# 如何从数据库中检索整数值&;将其存储在.net中的整数中
我想要数据库中的整数值试试看C# 如何从数据库中检索整数值&;将其存储在.net中的整数中,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我想要数据库中的整数值试试看 protected void Calendar1_DayRender(object sender, DayRenderEventArgs e) { SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "select FromYear from Factory where Cal_ID='" + DropDownList
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select FromYear from Factory where Cal_ID='" + DropDownList1.SelectedItem.Text + "'";
cmd.Connection = con;
con.Open();
dr = cmd.ExecuteReader();
dr.Read();
d = dr[0].ToString();
//d =(string) Label4.Text;
con.Close();
}
dr.GetInt32(0)
应在位置0读取一个int尝试
protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select FromYear from Factory where Cal_ID='" + DropDownList1.SelectedItem.Text + "'";
cmd.Connection = con;
con.Open();
dr = cmd.ExecuteReader();
dr.Read();
d = dr[0].ToString();
//d =(string) Label4.Text;
con.Close();
}
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select FromYear from Factory where Cal_ID='" + DropDownList1.SelectedItem.Text + "'";
cmd.Connection = con;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
int d = dr.GetInt32(0);
con.Close();
dr.GetInt32(0)
应在位置0处读取一个int
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select FromYear from Factory where Cal_ID='" + DropDownList1.SelectedItem.Text + "'";
cmd.Connection = con;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
int d = dr.GetInt32(0);
con.Close();
受保护的无效日历1\u DayRender(对象发送方,DayRenderReventArgs e)
{
SqlCommand cmd=新的SqlCommand();
cmd.CommandType=CommandType.Text;
cmd.CommandText=“从Cal_ID='”的工厂中选择FromYear”+DropDownList1.SelectedItem.Text+”;
cmd.Connection=con;
con.Open();
d=GetYear(cmd.ToString();
con.Close();
}
“肮脏”工作由GetYear完成:
私人建筑默认年=2000年;
private int GetYear(System.Data.IDbCommand){
int year=默认年份;
使用(System.Data.IDataReader reader=command.ExecuteReader()){
if(reader.Read()){
年份=GetInOrderFault(读卡器,0);
}
}
回归年;
}
私有int GetIntOrderFault(System.Data.IDataRecord记录,int序号){
if(记录IsDBNull(序号)){
返回默认年份;
}否则{
返回记录.GetInt32(序号);
}
}
受保护的无效日历1\u DayRender(对象发送方,DayRenderReventArgs e)
{
SqlCommand cmd=新的SqlCommand();
cmd.CommandType=CommandType.Text;
cmd.CommandText=“从Cal_ID='”的工厂中选择FromYear”+DropDownList1.SelectedItem.Text+”;
cmd.Connection=con;
con.Open();
d=GetYear(cmd.ToString();
con.Close();
}
“肮脏”工作由GetYear完成:
私人建筑默认年=2000年;
private int GetYear(System.Data.IDbCommand){
int year=默认年份;
使用(System.Data.IDataReader reader=command.ExecuteReader()){
if(reader.Read()){
年份=GetInOrderFault(读卡器,0);
}
}
回归年;
}
私有int GetIntOrderFault(System.Data.IDataRecord记录,int序号){
if(记录IsDBNull(序号)){
返回默认年份;
}否则{
返回记录.GetInt32(序号);
}
}
用法:
intd=dr.GetInt32[0]代码>同时检查:如果您愿意,为什么不直接执行以下操作:∫i=Convert.ToInt32(dr[0];int i=Convert.ToInt32(dr[0]);使用:int d=dr.GetInt32[0];
还要检查:如果需要,为什么不直接执行:∫i=Convert.ToInt32(dr[0];int i=Convert.ToInt32(dr[0]);请使用参数(“从Cal_ID=@param的工厂中选择FromYear”)避免字符串连接:请使用参数(“从Cal_ID=@param的工厂中选择FromYear”)避免字符串连接:
string InvAmountQuery = "SELECT InvAmount FROM Registration WHERE Email='" + useremail + "' ";
SqlCommand InvAmountcom = new SqlCommand(InvAmountQuery, conn);
int InvAmount = Convert.ToInt32(InvAmountcom.ExecuteScalar().ToString());