怎样把DataSet中的数据读出来形成字段插入到数据库???

fkueyygyispw 2009-09-23 09:59:21
现在我做一个系统大部分东西都是自定义的,我现在碰到一个问题是我有个dataset,我要把dataset中的数据取出来,形成一个像lable加TextBox组合,lable的内容是数据库动态读,lable的字段名和TextBox的ID是对应的,然后形成一条SQL Insert Into语句插入到数据库(注:DataSet数据不定,是从数据库动态读取)???
帮忙帮到底最好附上思路+全一点的代码!我先在这说谢了!
...全文
66 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
fkueyygyispw 2009-09-24
  • 打赏
  • 举报
回复
我是说怎样把读出来的数据形成一条整的插入语句?
hui_1019 2009-09-23
  • 打赏
  • 举报
回复
tie 哇,帖子版面全乱了,眼花了啊 先顶顶



polarissky 2009-09-23
  • 打赏
  • 举报
回复
调用

if (dataSet.Tables[0].Count > 0)
{
// This routine checks to see if the table exists in the
// database and creates the table if it does not exist
TableCheck();
// This routine inserts the data from the XML file into the database
TableInsert();
//If the message label is empty this means there were no errors
if (Message.Text == String.Empty)
{
// This message will display when complete if there were no errors
Message.Text = "Inserting XML into OleDb data source is complete.";
}
}
polarissky 2009-09-23
  • 打赏
  • 举报
回复
下面贴代码:

private void TableCheck()
{
// Create an OleDb database connection using the connection string
// provided when the web form is submitted
OleDbConnection oledbConn = new OleDbConnection(@"Provider=SQLOLEDB;Data Source=(local)\SQLEXPRESS; Initial Catalog=aspnetdb;User ID=sa;Password=123456;");
try
{
// Open the database connection
oledbConn.Open();
// Retrieve database schema information for only the table we are looking for
// In this example the table name is the name of the XML file without the extension
DataTable schemaTable = oledbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new object[] { null, null, tableName, "TABLE" });
String sqlCmd = "";
// Check to see if the table exists in the database schema
// If the table exists in the schema there will be 1 row in the DataTable
// If the table does not exist in the schema the DataTable row count will be zero
if (schemaTable.Rows.Count < 1)
{
// Make the create table sql command by iterating through the XML file's
// columns. This way the database columns will have the same name as the XML file.
sqlCmd = "create table " + tableName + " (";
for (int i = 0; i < dataTableXml.Columns.Count; i++)
{
// This adds each column as a text/string type with a length of 100
sqlCmd = sqlCmd + dataTableXml.Columns[i].ColumnName.ToString() + " char(100),";
}
// Remove the last ","
sqlCmd = sqlCmd.Substring(0, sqlCmd.Length - 1) + ");";
// Create and execute the create table command
OleDbCommand oledbCmd = new OleDbCommand(sqlCmd, oledbConn);
oledbCmd.ExecuteNonQuery();
}
}
catch
{
// If there are errors you will get this message
Message.Text = "The table could not be created or database does not exist.";
}
finally
{
// Close the database connection
oledbConn.Close();
}
}

private void TableInsert()
{
OleDbConnection oledbConn = new OleDbConnection(@"Provider=SQLOLEDB;Data Source=(local)\SQLEXPRESS; Initial Catalog=aspnetdb;User ID=sa;Password=123456;");
try
{
// Open the database connection
oledbConn.Open();
// Iterate rows in the dataset
foreach (DataRow dr in dataTableXml.Rows)
{
// Create the sql insert command for each row
string sqlCmd = "insert into [" + tableName + "] (";
string animalGuid = dr["动物GUID"].ToString();
if (!IsExistent(animalGuid))
{
// Iterate the datatable columns
for (int i = 0; i < dataTableXml.Columns.Count; i++)
{
// Add the column name
sqlCmd = sqlCmd + dataTableXml.Columns[i].ColumnName.ToString() + ",";
}
sqlCmd = sqlCmd.Substring(0, sqlCmd.Length - 1) + ") values (";
// Iterate the datatable columns
for (int x = 0; x < dataTableXml.Columns.Count; x++)
{
// Add the column value for this row
sqlCmd = sqlCmd + "'" + dr[x].ToString().Replace("'", "''") + "',";
}
sqlCmd = sqlCmd.Substring(0, sqlCmd.Length - 1) + ");";
}
// Create and execute the insert command
if (sqlCmd != "insert into [" + tableName + "] (")
{
OleDbCommand oledbCmd = new OleDbCommand(sqlCmd, oledbConn);
oledbCmd.ExecuteNonQuery();
}
}
}
catch
{
// If there are errors you will get this message
Message.Text = "There was an error adding the XML data to the table.";
}
finally
{
// Close the database connection
oledbConn.Close();
}
}
polarissky 2009-09-23
  • 打赏
  • 举报
回复
给你个思路:DataSet->DataTable->DataRow->DataColumn 构造SQL语句
超级大笨狼 2009-09-23
  • 打赏
  • 举报
回复
感觉你的思路是混乱的,没有分清楚界面和数据层的概念。

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧