哎 就说三层来说 如果我想 查询出用户名 密码 真实名字 号码 我先写一个方法查询出生出来return一次 再 一个方法转换字符return 再一个方法传送到页面 写 哎
我现在连接到数据库SqlConnection conn = new SqlConnection(connstr)
在SqlCommand cmd =。。。就写一个查询 return出去 在另外一个类里写转换字符怎么写 能说明白点吗
刚才那个例子不对,那是我计数用的
public TB_Nation SearchByName(string name)
{
TB_Nation nation = new TB_Nation();
//nation = null;
using (SqlConnection conn = new SqlConnection(connString))
{
SqlCommand command = new SqlCommand("select * from TB_Nation where NationName=@nationname ", conn);
command.CommandType = CommandType.Text;
command.Parameters.Add("@nationname", SqlDbType.VarChar).Value = name;
conn.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.Read())
{
nation.Id = int.Parse(reader["id"].ToString());
nation.Nationname = name;
}
}
conn.Close();
conn.Dispose();
}
return nation;
}
如果你想返回其他类型的值,比如string,int ,一样的道理,类型换一下就可以
你这个转换字符就是把数据库获取的数据,再进行一下数据转换等,格式化成你需要的格式,然后再继续传递
你的查询类里面有返回的数据,在你的转换类里面定义变量,将查询类的返回值直接赋值给转换类里面的变量就可以了。
public int GetNumber(TB_Nation obj)
{
int number = 0;
using (SqlConnection conn = new SqlConnection(connString))
{
string sql = "insert into TB_Nation(nationno,nationname) values(@nationno,@nationname)";
SqlCommand command = new SqlCommand(sql, conn);
command.CommandType = CommandType.Text;
command.Parameters.Add("@nationno", SqlDbType.Int).Value = obj.Nationno;
command.Parameters.Add("@nationname", SqlDbType.VarChar).Value = obj.Nationname;
conn.Open();
number = command.ExecuteNonQuery();
conn.Close();
}
return number;
}
这是个我写过的一个小程序,TB_Nation是数据库里的的表的模型。你看看有没有帮助