datatable 导入mysql 解决_将datatable 插入数据库

/*using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace jonastrysql

{

class Program

{

static void Main(string[] args)

{

}

}

}*/

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data.SqlClient;usingSystem.Collections;usingSystem.Data;namespaceConsoleApplication1

{classProgram

{static void Main(string[] args)

{string connectString = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=JonasTry;Integrated Security=True"; //"Data Source=leaf-home\\sqlserver2005;Initial Catalog=Test;Persist Security Info=True;User ID=site_dev;Password=???";

using (SqlConnection conn = newSqlConnection(connectString))

{

conn.Open();

Console.Write("ni hao");/*SqlCommand sqlCom = new SqlCommand();

sqlCom.Connection = conn;

sqlCom.CommandType = CommandType.Text;

sqlCom.CommandText = "select * from [JonasTry].[DBO].[tb]";

SqlDataReader tableList = sqlCom.ExecuteReader();

while (tableList.Read())

{

Console.Write(tableList["A"].ToString()+"\t");

Console.Write(tableList["B"].ToString() + "\t");

Console.Write("\n");

}

tableList.Close();*/

//-------- 创建要测试的数据 begin -------------

DataTable dtNew = newDataTable();

dtNew.Columns.Add(new DataColumn("A", typeof(int)));

dtNew.Columns.Add(new DataColumn("B", typeof(string)));

DataRow dr=dtNew.NewRow();

dr["A"] = 99;

dr["B"] = "酒";

dtNew.Rows.Add(dr);

dr=dtNew.NewRow();

dr["A"] = 101;

dr["B"] = "百";

dtNew.Rows.Add(dr);//-------- 创建要测试的数据 end -------------//得到原表结构//conn.Open();

string strSql = "SELECT * FROM dbo.tb WHERE 1=0";

SqlDataAdapter adapter= newSqlDataAdapter(strSql, conn);

SqlCommandBuilder builder= newSqlCommandBuilder(adapter);

DataSet ds= newDataSet();

adapter.Fill(ds,"flag");

DataTable table= ds.Tables["flag"];//将新数据传到对应的datatable中

foreach (DataRow drNew indtNew.Rows)

{

DataRow row=table.NewRow();foreach (DataColumn dc intable.Columns)

{

row[dc.ColumnName]=drNew[dc.ColumnName];

}

table.Rows.Add(row);

}//更新数据库

adapter.Update(table);//读取数据库中的数据

SqlCommand sqlCom = newSqlCommand();

sqlCom.Connection=conn;

sqlCom.CommandType=CommandType.Text;

sqlCom.CommandText= "select * from [JonasTry].[DBO].[tb]";

SqlDataReader tableList=sqlCom.ExecuteReader();while(tableList.Read())

{

Console.Write(tableList["A"].ToString() + "\t");

Console.Write(tableList["B"].ToString() + "\t");

Console.Write("\n");

}

tableList.Close();//读完了

Console.Write("\n show finish");

}

Console.Read();

}//end of Main

}//end of class

}//end of namespace

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值