本文介绍了如何在数据库中的diferrent-2行中保存多个文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
How to save Multiple textbox values in diferrent-2 Rows in Database at a time-
Like-
Textbox1.Text="John";
Textbox1.Text="Marcus";
Textbox1.Text="Megan";
Sql Table-
ID Name
1 John
2 Marcus
3 Megan
Can Anybody tell me the solution plz...
推荐答案
class MultiPleValues
{
DataTable dt;
private void Insert()
{
try
{
dt = GetDataTable();
//You can use following in loop. or as your requirement.
DataRow dr = dt.NewRow();
dr.BeginEdit();
dr["S_NO"] = "You Values";
dr["Name"] = "Your Name";
dr.EndEdit();
dt.Rows.Add(dr);
//After your work compeletition.
//Insert dt in database useing following method:
string strQuer = "Select * from --Your Table Name Here";
dt = InsertQuery(strQuer, dt); //this will insert your values in db and also select modified values
}
catch (Exception)
{
throw;
}
}
private DataTable GetDataTable()
{
try
{
DataTable dtTemp = new DataTable();
dtTemp.Columns.Add("S_NO");
dtTemp.Columns.Add("Name");
return dtTemp;
}
catch (Exception)
{
throw;
}
}
public static DataTable InsertQuery(string SelectQuery, DataTable dt)
{
try
{
SqlConnection sqlconn = new SqlConnection("Your Connection String here");
//this function will insert changes in DB, also select updated values.
sqlconn.Open();
SqlCommand sqlcommand = new SqlCommand(SelectQuery, sqlconn);
SqlDataAdapter adapter = new SqlDataAdapter(sqlcommand);
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(adapter);
cmdBuilder.ConflictOption = ConflictOption.OverwriteChanges;
adapter.Update(dt);
return dt;
}
catch (Exception)
{
throw;
}
}
}
这篇关于如何在数据库中的diferrent-2行中保存多个文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!