简体   繁体   English

使用 ADO.NET 在代码隐藏 C# 中使用 select 语句获取所有行数据

[英]Get all the row data with select statement in codebehind C# using ADO.NET

Maybe someone can help me with the following problem.也许有人可以帮助我解决以下问题。 what i want to achieve is to get all rows of a table and put them in a string with comma between each value.我想要实现的是获取表的所有行并将它们放入一个字符串中,每个值之间带有逗号。 The result should be a scriptfile to get all of the rowdata in the table.结果应该是一个脚本文件,用于获取表中的所有行数据。

For so far:到目前为止:

static void Main(string[] args) {
    StringBuilder builder = new StringBuilder();
    string ConString = @"Data Source=.;Initial Catalog=USR;Integrated Security=True";
    foreach (string itemtablename in TableNames())
    {
        Schema schema = new Schema();
        schema.TableName = itemtablename; //the name of the table

        //int i = 0;
        string queryvalues = "select * from " + itemtablename;
        SqlConnection conValues = new SqlConnection(ConString);
        using (conValues)
        {
            conValues.Open();
            SqlCommand cmdSchemaValues = new SqlCommand(queryvalues, conValues);
            SqlDataReader readerSchemaValues = cmdSchemaValues.ExecuteReader();
            DataTable dataTable = readerSchemaValues.GetSchemaTable();

            //builder.AppendLine("INSERT INTO " + itemtablename);
            //builder.AppendLine(" VALUES (");

            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                string rowValues= dataTable.Rows[i].ToString() + ",";
               
                builder.Append(rowValues);
            }
        }
    }
    System.IO.File.WriteAllText(@"C:\script.txt", builder.ToString());
}

If "generating the insert script for data present in the table" is the objective then you can try using the SQL Server Management Studio to do that.如果目标是“为表中存在的数据生成插入脚本”,那么您可以尝试使用 SQL Server Management Studio 来执行此操作。 It has been discussed in other Stack Overflow questions as well.它也已在其他Stack Overflow 问题中讨论过。

Try this logic:试试这个逻辑:

foreach(DataRow row in datatable.Rows)
{
    builder.AppendLine("INSERT INTO " + itemtablename + "(" + string.Join(", ", datatable.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToArray()).Trim().TrimEnd(","));
    builder.AppendLine("VALUES ('" + string.Join("', '", row.ItemArray).Trim().TrimEnd('\'').Trim().TrimEnd(',') + "')")
}

string script = builder.ToString();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在ADO.NET / C#中使用DbTransaction上的using语句? - Why to use using statement on DbTransaction in ADO.NET / C#? 如何使用C#ADO.NET更新Excel行? - How to update a excel row using C# ADO.NET? 使用C#从Excel到数据库ADO.net的导入数据中选择空格表 - select spacific sheet in import data from Excel to database ADO.net using C# 如何使用C#ADO.Net从多行SQL Server插入命令获取输出? - How to get output from SQL Server Insert Command with more than one row using C# ADO.Net? 如何在C#中使用ODBC读取特定Excel工作表中的所有数据行到ADO.NET数据表中 - How to read all data rows in a specific Excel sheet into an ADO.NET Datatable with out using ODBC in C# C#使用SqlKata(或者只是ADO.NET)从动态对象到SQL插入语句 - C# Getting from dynamic object to SQL Insert statement using SqlKata (or alternatively just ADO.NET) 如何使用C#ADO.NET中的参数将行向上插入DB2 iSeries? - How to upsert row into DB2 iSeries using parameters in C# ADO.NET? 使用ado.net在C#中的datagridview中插入数据库检查的行 - insert to database checked row in datagridview in c# using ado.net 在C#中,与ADO.NET结合使用的“ SELECT TOP 0 * FROM(/ *…* /)s”是确定SELECT语句中列信息的好方法吗? - In C#, is “SELECT TOP 0 * FROM (/* … */) s” used in conjuction with ADO.NET a good way to determine the column information in a SELECT statement? 如何将excel文件一列的所有内容读入数组C#? 不使用 ADO.net 方法 - How to read all contents of a column of excel file into an array C#? Without using ADO.net approach
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM