如何从文本框中获取要保存的(从SQL DB)数据 [英] How do I get the saved(from SQL DB) data to retrieve in a text box
本文介绍了如何从文本框中获取要保存的(从SQL DB)数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有文本框和保存按钮
So i have Textboxs and a save button
string FemaleCondoms = txtFemaleCondoms.Text;
string MaleCondoms = txtMaleCondoms.Text;
string Lube = txtLube.Text;
string HTS = txtHTS.Text;
string IEC = txtIEC.Text;
<asp:Button ID="btnSave" runat="server" Text="Save Data" OnClick ="SaveData_Click" />
当我点击保存数据时,必须将信息发送到数据库并在文本框中检索该信息。没有加载
我尝试过:
When ever i click on save data , info must be sent to DB and Retrieve that info on textboxes. without loading
What I have tried:
//using (SqlConnection con = new SqlConnection(Initialize.ConnectString))
//{
// if (con.State == System.Data.ConnectionState.Closed)
// {
// con.Open();
// }
// using (SqlCommand cmd = new SqlCommand("Select FemaleCondoms from OutreachTotalCommodityDistributedPerOutreach Where FemaleCondoms=@FemaleCondoms", con))
// {
// cmd.Parameters.AddWithValue("@FemaleCondoms", FemaleCondoms);
// cmd.Parameters.AddWithValue("@MaleCondoms", MaleCondoms);
// cmd.Parameters.AddWithValue("@Lube", Lube);
// cmd.Parameters.AddWithValue("@IEC", IEC);
// cmd.Parameters.AddWithValue("@HTSKits", HTS);
// using (SqlDataReader rd = cmd.ExecuteReader())
// {
// if(rd.Read() )
// {
// txtFemaleCondoms.Text = rd["FemaleCondoms"].ToString(); }
// }
// }
推荐答案
有很多例子可以显示如何使用ADO.NET执行CRUD。你只需要在谷歌找到它们。
作为参考,这是一个简单的例子:
要插入,你可以这样做:
There are tons of examples that shows how to perform CRUD using ADO.NET. You just have to find them at google.
As a reference, here's a quick example:
To Insert, you can do:
protected void InsertData(){
using(SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE")){
string sql = "INSERT INTO YourTableName (Field1,Field2,Field3) VALUES (@Param1,@Param2,@Param3)";
using(SqlCommand cmd = new SqlCommand(sql,connection)){
cmd.Parameters.AddWithValue("@Param1", TextBox1.Text)
cmd.Parameters.AddWithValue("@Param2", TextBox2.Text)
cmd.Parameters.AddWithValue("@Param3", TextBox2.Text)
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
}
}
}
要选择,你可以执行:
To Select, you can do:
protected void FetchData(string searchText){
using(SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING HERE")){
string sql = "SELECT Field1, Field2, Field3 FROM TableName WHERE YourFieldName = @Param1";
using(SqlCommand cmd = new SqlCommand(sql,connection)){
cmd.Parameters.AddWithValue("@Param1", searchText);
DataTable dt = new DataTable();
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(dt);
//check if the query returns any data
if (dt.Rows.Count > 0) {
TextBox1.Text = dt.Rows[0]["Field1"].ToString();
TextBox2.Text = dt.Rows[0]["Field2"].ToString();
TextBox3.Text = dt.Rows[0]["Field3"].ToString();
}
else
{
//No records found
}
}
}
}
这篇关于如何从文本框中获取要保存的(从SQL DB)数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文