使用 ADO.NET 在代码隐藏 C# 中使用 select 语句获取所有行数据
Get all the row data with select statement in codebehind C# using ADO.NET
也许有人可以帮我解决以下问题。
我想要实现的是获取 table 的所有行并将它们放入一个字符串中,每个值之间用逗号分隔。结果应该是一个脚本文件,用于获取 table.
中的所有行数据
到目前为止:
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());
}
如果“为 table 中存在的数据生成插入脚本”是 objective,那么您可以尝试使用 SQL Server Management Studio 来执行此操作。其他 Stack Overflow questions 也讨论过。
试试这个逻辑:
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();
也许有人可以帮我解决以下问题。 我想要实现的是获取 table 的所有行并将它们放入一个字符串中,每个值之间用逗号分隔。结果应该是一个脚本文件,用于获取 table.
中的所有行数据到目前为止:
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());
}
如果“为 table 中存在的数据生成插入脚本”是 objective,那么您可以尝试使用 SQL Server Management Studio 来执行此操作。其他 Stack Overflow questions 也讨论过。
试试这个逻辑:
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();