asp.net用c#怎么从sql server2005 中取出数据 显示在label1中?在线等!!!
我写的代码是:using(SqlConnectioncon1=newSqlConnection(ConfigurationManager.ConnectionString...
我写的代码是:using (SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString))
{
con1.Open();
SqlCommand com = new SqlCommand("SELECT max(t_id) from [vot_main]", con1); ;
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
this.Label2.Text =dr[""].ToString();
}
主要实现取出vot_main表中t_id(int型)的最大值?
wzh8514 :看了你的答案 但报错啊
“编译器错误信息: CS0246: 找不到类型或命名空间名称“HashTable”(是否缺少 using 指令或程序集引用?)”
这个怎么办? 展开
{
con1.Open();
SqlCommand com = new SqlCommand("SELECT max(t_id) from [vot_main]", con1); ;
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
this.Label2.Text =dr[""].ToString();
}
主要实现取出vot_main表中t_id(int型)的最大值?
wzh8514 :看了你的答案 但报错啊
“编译器错误信息: CS0246: 找不到类型或命名空间名称“HashTable”(是否缺少 using 指令或程序集引用?)”
这个怎么办? 展开
5个回答
展开全部
用DataTable
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ArrayList record = new ArrayList();
using (SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString))
{
con1.Open();
SqlCommand com = new SqlCommand("SELECT max(t_id) id from [vot_main]", con1); ;
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
Hashtable field = new Hashtable();
for (int i = 0; i < dr.FieldCount; i++)
{
field.Add(dr.GetName(i), dr.GetSqlValue(i).ToString());
}
record.Add(field);}
}
if(record.Count>0)
this.Label2.Text=((HashTable)record[0])["id"].ToString();
using (SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString))
{
con1.Open();
SqlCommand com = new SqlCommand("SELECT max(t_id) id from [vot_main]", con1); ;
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
Hashtable field = new Hashtable();
for (int i = 0; i < dr.FieldCount; i++)
{
field.Add(dr.GetName(i), dr.GetSqlValue(i).ToString());
}
record.Add(field);}
}
if(record.Count>0)
this.Label2.Text=((HashTable)record[0])["id"].ToString();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你个参考代码,你看看吧。 string Computername = System.Net.Dns.GetHostName();
string connectSql = "Data Source=" + Computername + ";Initial Catalog=WebBank//要连接的数据库;Integrated Security=True";
SqlConnection sqlConnection = new SqlConnection(connectSql);
sqlConnection.Open();
string sqlCommand1 = "select Password from AgricultureBank where cardId=" + Convert.ToInt32(textBox3.Text);
//查询语句
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand1, sqlConnection);
DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds);
string password = ds.Tables[0].Rows[0]["password"].ToString();
this.textBox1.Text=password;
sqlConnection.Close();
把密码从数据库里拿出后赋值给textBox1.Text
这是已经被采纳的答案,经多人试验成功.呵呵,给分吧
string connectSql = "Data Source=" + Computername + ";Initial Catalog=WebBank//要连接的数据库;Integrated Security=True";
SqlConnection sqlConnection = new SqlConnection(connectSql);
sqlConnection.Open();
string sqlCommand1 = "select Password from AgricultureBank where cardId=" + Convert.ToInt32(textBox3.Text);
//查询语句
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand1, sqlConnection);
DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds);
string password = ds.Tables[0].Rows[0]["password"].ToString();
this.textBox1.Text=password;
sqlConnection.Close();
把密码从数据库里拿出后赋值给textBox1.Text
这是已经被采纳的答案,经多人试验成功.呵呵,给分吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using (SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString))
{
con1.Open();
SqlCommand com = new SqlCommand("SELECT max(t_id) from [vot_main]", con1); ;
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
this.Label2.Text =dr[""].ToString();
}
改成
using (SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString))
{
con1.Open();
SqlCommand com = new SqlCommand("SELECT max(t_id) from [vot_main]", con1); ;
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
this.Label2.Text =dr[0].ToString();
}
看到改的哪了吗?
dr[""].ToString改成dr[0].ToString
{
con1.Open();
SqlCommand com = new SqlCommand("SELECT max(t_id) from [vot_main]", con1); ;
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
this.Label2.Text =dr[""].ToString();
}
改成
using (SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString))
{
con1.Open();
SqlCommand com = new SqlCommand("SELECT max(t_id) from [vot_main]", con1); ;
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
this.Label2.Text =dr[0].ToString();
}
看到改的哪了吗?
dr[""].ToString改成dr[0].ToString
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询