从database.edit获取不同的表数据gridview中的数据并将其存储到数据库中。当表中的列数不固定时。 [英] Get the different table data from database.edit the data in the gridview and store it into the database.when number of column in the table is not fixed.

查看:75
本文介绍了从database.edit获取不同的表数据gridview中的数据并将其存储到数据库中。当表中的列数不固定时。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目。我有两个下拉列表和一个gridview.and选择了一个数据库。数据库中存在的所有表应该出现在Second DropdownList中。现在我想做什么从下拉列表中选择一个表格。应该使用所选表格中的数据填充gridview。

现在,我想在Gridview中给出编辑选项。

编辑后应该将数据存储到数据库中。

我的主要问题是,表列名称是固定的。有时候是2列,有时是3,有时是4及其变量。没有得到任何我可以完成这项工作。我可以坚持到这里。提前装好。



我尝试了什么:



用于在下拉列表中获取表名。

  protected   void  DropDownList3_SelectedIndexChanged(< span class =code-keyword> object  sender,EventArgs e)
{
try
{
使用(SqlConnection conn = ReturnConnection2(ConString3))
{
SqlDataAdapter da = new SqlDataAdapter( USE + DropDownList3.Text.ToString()+ ; SELECT name FROM sys.Tables;,conn);
DataTable ds = new DataTable();
da.Fill(ds);
DropDownList4.DataSource = ds;
DropDownList4.DataTextField = name;
DropDownList4.DataValueField = name;
DropDownList4.DataBind();
conn.Close();
DropDownList4.Items.Insert( 0 new ListItem( 选择表名 选择表名));
}
}
catch
{

}
}
// 填充网格
protected void DropDownList2_SelectedIndexChanged( object sender,EventArgs e)
{
< span class =code-keyword> try
{
string central_string;
使用(SqlConnection conn = ReturnConnection())
{
SqlDataAdapter cmd = new SqlDataAdapter( SELECT server_name,server_type,user_ID,password FROM server_name where server_name =' + DropDownList5.SelectedValue + ',conn);
conn.Open();
DataTable dt = new DataTable();
cmd.Fill(dt);

string Datasource = 数据源= + dt.Rows [ 0 ] [ 0 ]。ToString() ;
string user_ID = 用户ID = + dt.Rows [ 0 ] [ 2 ]。ToString();
string password = 密码= + dt.Rows [ 0 ] [ 3 ]。ToString();
string Initial_catalog = Initial Catalog = + DropDownList1.Text;
central_string =数据源+ ; + user_ID + ; +密码+ ; + Initial_catalog;

}
loadgrd(central_string);
}
catch
{

}
}



如何进行列名更改的编辑没有得到。

请帮助。提前谢谢。

解决方案

< blockquote>我建​​议使用 DataSet [ ^

MSDN文档说明:

ADO.NET DataSet是一种内存驻留的数据表示,提供一致的关系编程模型,不管它包含的数据源。 DataSet表示一组完整的数据,包括包含,排序和约束数据的表,以及表之间的关系。





因此,您可以在应用程序启动时连接数据库。当用户选择特定的表时,您需要的只是获得 DataTable [ ^ ]来自 DataSet 的对象通过简单的指令:



< pre lang =c#> DataTable dt = YourDataSet.Tables [ TableName];
YourGridView.DataSource = dt;





DataSet和DataTable对象有几种方法可以将更新的数据发布回数据库,例如: DataSet.AcceptChanges Method(System.Data) [ ^ ]



详情请见: DataSet,DataTables和DataViews | Microsoft Docs [ ^ ]



祝你好运!


I am working on a project.where i have two dropdown list and one gridview.and After selecting the one of the database .All the tables present in the database,should appear in the Second DropdownList.Now what i wanted to do is .After selecting a table from dropdown.The gridview should be populated with the data available in the selected table.
Now,I wanted to give edit option in the Gridview .
After editing it should store the data into the database.
my main problem is that,the table column is name fixed.Some time 2 column,sometime 3 ,sometime 4 and its variable.Not getting any idea .Please can anyone sujjest me.How can i complete this work.I m stuck here.Tanks in advance.

What I have tried:

for taking table name in dropdown.

protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                using (SqlConnection conn = ReturnConnection2(ConString3))
                {
                    SqlDataAdapter da = new SqlDataAdapter("USE " + DropDownList3.Text.ToString() + ";SELECT name FROM sys.Tables ;", conn);
                    DataTable ds = new DataTable();
                    da.Fill(ds);
                    DropDownList4.DataSource = ds;
                    DropDownList4.DataTextField = "name";
                    DropDownList4.DataValueField = "name";
                    DropDownList4.DataBind();
                    conn.Close();
                    DropDownList4.Items.Insert(0, new ListItem("Select Table Name", "Select Table Name"));
                }
            }
            catch
            {

            }
        }
//populate grid
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string central_string;
                using (SqlConnection conn = ReturnConnection())
                {
                    SqlDataAdapter cmd = new SqlDataAdapter("SELECT server_name,server_type,user_ID,password FROM  server_name where server_name='" + DropDownList5.SelectedValue + "'", conn);
                    conn.Open();
                    DataTable dt = new DataTable();
                    cmd.Fill(dt);

                    string Datasource = "Data Source=" + dt.Rows[0][0].ToString();
                    string user_ID = "User ID=" + dt.Rows[0][2].ToString();
                    string password = "Password=" + dt.Rows[0][3].ToString();
                    string Initial_catalog = "Initial Catalog=" + DropDownList1.Text;
                    central_string = Datasource + ";" + user_ID + ";" + password + ";" + Initial_catalog;

                }
                loadgrd(central_string);
            }
            catch
            {

            }
        }


How to give edit for column name changing is not getting.
please help.Thanks in advance.

解决方案

I'd suggest to use DataSet[^].

MSDN documentation states:

The ADO.NET DataSet is a memory-resident representation of data that provides a consistent relational programming model regardless of the source of the data it contains. A DataSet represents a complete set of data including the tables that contain, order, and constrain the data, as well as the relationships between the tables.



So, you can connect with your database when an application starts. When user will select specific table, all what you need is to get a DataTable[^] object from DataSet through the simple instruction:

DataTable dt = YourDataSet.Tables["TableName"];
YourGridView.DataSource = dt;



DataSet and DataTable objects have several methods to post updated data back to database, for example: DataSet.AcceptChanges Method (System.Data)[^]

For further details, please see: DataSets, DataTables, and DataViews | Microsoft Docs[^]

Good luck!


这篇关于从database.edit获取不同的表数据gridview中的数据并将其存储到数据库中。当表中的列数不固定时。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆