我的数据表:
字段 数据类型 备注
ID int (key)
clientIP char
clientBrowser char
clientSystem char
clientTime datetime我老是写错SQl语句,我想写个函数
public void TableModify(string ID,string clientIP,string clientBrowser,string clientSystem, string clientTime){}
有劳各位大虾帮忙写个函数体啊!!!实现对表中数据的修改!
字段 数据类型 备注
ID int (key)
clientIP char
clientBrowser char
clientSystem char
clientTime datetime我老是写错SQl语句,我想写个函数
public void TableModify(string ID,string clientIP,string clientBrowser,string clientSystem, string clientTime){}
有劳各位大虾帮忙写个函数体啊!!!实现对表中数据的修改!
string sql ="update Table Set Ip = "+clientIP+",Browers="+clientBrowser+",clientSystem="+clientSystem+",clientTime="+clientTime+" where ID="+ID;SqlConnection conn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
conn.open();
cmd.ExecuteQuery();}
string sql ="update Table Set Ip = "+clientIP+",Browers="+clientBrowser+",clientSystem="+clientSystem+",clientTime="+clientTime+" where ID="+ID;SqlConnection conn = new SqlConnection(ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
conn.open();
cmd.ExecuteQuery();
conn.close();//請關閉。:)
}
语句都有了,调用Adapter.UpDate(ds) 方法一切搞定!,如果你愿意自己写也没问题,把那个里面自动生成的Command语句Copy出来:)不过有一点要注意,组建类自动生成时SqlConnection的ConnectionString是写死的,建议动态改
{
SqlConnction conn=new SqlConnection("server=localhost;uid=sa;pwd=;database=yourdb");
SqlCommand cmd=new SqlCommand("update tablename set clientIP=@clientIP,clientBrowser=@clientBrowser,.... where ID=@ID",cnn);
cmd.Parameters.Add("@clientIP",clientIP);
.....
cmd.Parameters.Add("@ID",ID);
try
{
cnn.open();
cmd.ExecuteNonquery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}}
不过我现在还是举步艰难,困惑如下:
//*********************
现在我目前的困境是这样的:
1)页面上有3个TextBox 和一个Button(修改数据库的按钮)
TextBox tbCid;TextBox tbClient;TextBox tbDatetime;Button Button1;
2)每次加载页面的时候读数据库并把一条记录的相关信息写入3个TextBox ;
然后我修改:TextBox tbClient;TextBox tbDatetime 中的字符、点吉Button Button1
向数据库中跟新数据。
发现数据库中数据没有修改,由这一句输出:Response.Write(sql + "<hr>");
发现其中的:TextBox tbClient;TextBox tbDatetime 中的数据还是页面加载时的值,并没有
随着我的跟改而改变!
具体代码如下:
//***********************
private void Page_Load(object sender, System.EventArgs e)
{
InitData();
} private void Button1_Click(object sender, System.EventArgs e)
{
string sql = "update Client set clientIP='" + tbClient.Text +
"'clientTime='" + tbDatetime.Text + "' "+"where ID="+tbCid.Text;
SqlCommand cmd = new SqlCommand();
cmd.Connection = GetConnection();
cmd.CommandText = sql;
Response.Write(sql + "<hr>");
try
{
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
Response.Write(ex.Message + "\t" + ex.StackTrace);
}
Response.Write("success");
}
} public void InitData()
{
string sql = " select ID,clientIP,clientTime from Client ";
DataTable dt = new DataTable("Client");
try
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(sql,GetConnection());
da.Fill(dt);
}
catch(Exception e)
{
dt = null;
Response.Write(e.Message + "\t" + e.StackTrace); }
tbCid.Text = dt.Rows[0]["ID"].ToString();
tbClient.Text = dt.Rows[0]["clientIP"].ToString();
tbDatetime.Text = dt.Rows[0]["clientTime"].ToString();
} public SqlConnection GetConnection()
{
SqlConnection sqlConnection;
sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = "Server=(local);user=sa;password=1;database=pubs";;
try
{
sqlConnection.Open();
}
catch(Exception e)
{
Response.Write(e.Message + "\t" + e.StackTrace);
return null;
}
return sqlConnection;
}
}
if(!IsPostBack)
{
InitData();
}
{
if(!IsPostBack)
{
InitData();
}
}原因很简单,因为你点更新按钮时,postback也同时要pageload,用
(!IsPostBack)过滤点postback回来的动作.
{
if(!IsPostBack)
{
InitData();
}
}
建议你多看看书,这些都是最基本的。
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
InitData();
}
}
大侠们的指导小弟一定铭记心中.
一直在看CET-6刚过,现在好好学习编程.
低起点..