如何把后台数据库的值,对应的传递到Form的txtbox(txt_bookno,txt_bookname,txt_author.......)中?
string str = "user id=sa;pwd=sa;database=图书管理系统;server=127.0.0.1;";
SqlConnection con = new SqlConnection(str);
con.Open(); string sql = "select* from [tushu] where bookno='"+txt_bookno .Text +"'";
SqlCommand com = new SqlCommand(sql, con); SqlDataAdapter adapter = new SqlDataAdapter(com);
DataSet dataset = new DataSet();
adapter.Fill(dataset, "tushu"); SqlDataReader read = com.ExecuteReader();
if (read .Read ()) //先连接数据库,后对应查询(根据bookno),有的话就显示出来
{
DataTable table=new DataTable ("tushu"); txt_bookno.Text = table .Rows [0]["bookno"].ToString();
txt_bookname.Text = table .Rows [0]["bookname"].ToString();
txt_author.Text = table .Rows [0]["author"].ToString();
txt_publish.Text = table .Rows [0]["publish"].ToString();
txt_rtime.Text = table .Rows [0]["rtime"].ToString();
txt_number.Text = table .Rows [0]["number"].ToString();
adapter.Update(table );
}
//我在做更新数据库操作,首先在Form的txtbox显示你要更新的内容,更新后保存到数据库中的代码?谢谢
string str = "user id=sa;pwd=sa;database=图书管理系统;server=127.0.0.1;";
SqlConnection con = new SqlConnection(str);
con.Open(); string sql = "select* from [tushu] where bookno='"+txt_bookno .Text +"'";
SqlCommand com = new SqlCommand(sql, con); SqlDataAdapter adapter = new SqlDataAdapter(com);
DataSet dataset = new DataSet();
adapter.Fill(dataset, "tushu"); SqlDataReader read = com.ExecuteReader();
if (read .Read ()) //先连接数据库,后对应查询(根据bookno),有的话就显示出来
{
DataTable table=new DataTable ("tushu"); txt_bookno.Text = table .Rows [0]["bookno"].ToString();
txt_bookname.Text = table .Rows [0]["bookname"].ToString();
txt_author.Text = table .Rows [0]["author"].ToString();
txt_publish.Text = table .Rows [0]["publish"].ToString();
txt_rtime.Text = table .Rows [0]["rtime"].ToString();
txt_number.Text = table .Rows [0]["number"].ToString();
adapter.Update(table );
}
//我在做更新数据库操作,首先在Form的txtbox显示你要更新的内容,更新后保存到数据库中的代码?谢谢
dataset.Tables[0]["bookname"]= txt_bookname.Text ;
....
System.Data.SqlClient.SqlCommandBuilder cb=new System.Data.SqlClient.SqlCommandBuilder(adapter)
adapter.Update(dataset,"tushu"); //更新到数据库
上面那段代码是将数据库传递到Form的txtbox。(错误的!)
我就是想大侠们,给出自己的思想和代码?
你这是重新new了个table,你这程序不出异常? 根本访问第0行就不行,
你把table换成 table= dataset.Tables[0]
【主要:(首先在Form的txtbox显示你要更新的内容),(次要)更新后保存到数据库中的代码】
我帮你用参数解决!~
public users Get(int UID)
{
string strSql =string.Format("SELECT * FROM users WHERE u.UID={0} ",UID);
using (SqlDataReader sdr = SQLHelper.ExecuteReader(connString, CommandType.Text,strSql, (SqlParameter[])null))
{
if (sdr.Read())
{
users model = new users();
model.UID = sdr.GetInt32(0);
model.name = sdr.GetString(1);
return model;
}
}
return null;
}
//使用的时候
users model=Get(123);
textBoxName.Text=model.name;
{
SqlParameter[] parms = new SqlParameter[]{
new SqlParameter("@UID",SqlDbType.Int),
new SqlParameter("@name",SqlDbType.NVarChar)
};
parms[0].Value = userModel.UID;
parms[1].Value = userModel.name;
SQLHelper.ExecuteNonQuery(connString, CommandType.StoredProcedure, "BBS_Users_Update", parms);
}
string connString = "server=.;database=数据库名;uid=sa;pwd=pk;";
SqlConnection connection = new SqlConnection(connString); string sql = "SQL语句";
try
{
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
int i = command.ExecuteNonQuery();
if (i == 1)
{
MessageBox.Show("更新成功~");
}
else
{
MessageBox.Show("更新失败");
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message.ToString());
}
finally {
connection.Close();
}
SqlConnection con = new SqlConnection(str);
con.Open();
string update = "update [tushu] set bookname='"+txt_bookname .Text +"',author='"+txt_author .Text+ "',publish='"+txt_publish .Text +"',rtime='"+txt_rtime .Text+ "',number='"+txt_number .Text +"'"; SqlCommand command = new SqlCommand(update , con);
int iResult = command.ExecuteNonQuery();
if(iResult>0)
{
MessageBox.Show("更新成功!","提示");
}
else
{
MessageBox.Show("更新操作失败","失败"); }
//怎么把数据库所有的都修改啦!不是一行的修改!
//怎么把数据库所有的都修改啦!不是一行的修改!
这个没重来没遇到过,看来你真的要等高手来了!~