VB.NET将datagridview的数据插入数据库

VB.NET将datagridview的数据插入数据库,第1张

Dim strConn As String = "driver={microsoft access driver (mdb)};uid=admin;pwd=;dbq=" &

ApplicationStartupPath & "\ACCESSMDB"

Dim cn As New OdbcConnection(strConn)

cnOpen()

Dim da As New OdbcDataAdapter("Select From 表名", cn)

Dim ds As New DataSet

daFill(ds, "表名")

Dim cmdBuilder As New OdbcCommandBuilder(da)

daInsertCommand = cmdBuilderGetInsertCommand

With dsTables("表名")

For i = 0 to DataGridView1RowsCount - 1

Dim dr As DataRow = NewRow

dr("列名1") = DataGridView1中的值

dr("列名n") = DataGridView1中的值

RowsAdd(dr)

next

End With

daUpdate(dsTables("表名"))

cnClose()

using System;

using SystemCollectionsGeneric;

using SystemComponentModel;

using SystemData;

using SystemDataSqlClient;

using SystemDrawing;

using SystemText;

using SystemWindowsForms;

namespace DataSource

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private DataSet ds = new DataSet();

private SqlConnection conn = null;

private SqlDataAdapter da = null;

private const string DRIVER = "server=;database=northwind;uid=sa;pwd=sa";

private const string sql_select = "select from region";

/

方法为将数据库northwind中的region表的数据查询出来并放入DataSet中

/

private void Form1_Load(object sender, EventArgs e)

{

conn = new SqlConnection(DRIVER);

da = new SqlDataAdapter(sql_select,conn);

daFill(ds,"table");

thisdataGridView1DataSource = dsTables["table"]DefaultView;

}

private bool BtnInsert() //此方法作用于添加

{

daInsertCommand = connCreateCommand();

daInsertCommandCommandText = "insert into region values(@id,@ption)";

daInsertCommandParametersAdd("@id", SqlDbTypeInt, 4, "regionid");

daInsertCommandParametersAdd("@ption", SqlDbTypeVarChar, 10, "regiondescription");

int count = daUpdate(ds);

bool result = count > 0 true : false;

return result;

}

private void button1_Click(object sender, EventArgs e)

{

if (thisBtnInsert())//调用此方法

{

MessageBoxShow("添加成功!");

}

else

{

MessageBoxShow("添加失败!");

}

}

private bool BtnDelect() //此方法作用于删除

{

SqlParameter sp = new SqlParameter();

daDeleteCommand = connCreateCommand();

daDeleteCommandCommandText = "delete region where regionid=@id";

sp = daDeleteCommandParametersAdd("@id", SqlDbTypeInt, 4, "regionid");

spSourceVersion = DataRowVersionOriginal;

dsTables["table"]Rows[thisdataGridView1CurrentRowIndex]Delete();

int count = daUpdate(ds);

bool result = count > 0 true : false;

return result;

}

private void button2_Click(object sender, EventArgs e)

{

if (thisBtnDelect())//调用删除方法

{

MessageBoxShow("删除成功!");

}

else

{

MessageBoxShow("删除失败!");

}

}

private bool BtnUpdate() //此方法作用于修改

{

SqlParameter sp = new SqlParameter();

daUpdateCommand = connCreateCommand();

daUpdateCommandCommandText = "update region set regionid=@id,regiondescription=@ption where regionid=@oldid";

daUpdateCommandParametersAdd("@id", SqlDbTypeInt, 4, "regionid");

daUpdateCommandParametersAdd("@ption", SqlDbTypeVarChar, 10, "regiondescription");

sp = daUpdateCommandParametersAdd("@oldid", SqlDbTypeInt, 4, "regionid");

spSourceVersion = DataRowVersionOriginal;

int count = daUpdate(ds);

bool result = count > 0 true : false;

return result;

}

private void button3_Click(object sender, EventArgs e)

{

if (thisBtnUpdate())//调用修改方法

{

MessageBoxShow("修改成功!");

}

else

{

MessageBoxShow("修改失败!");

}

}

}

}

winform还是net?如果是winfrom参考下面的代码吧!

public void ExcelIntoDatagridView()

{

//打开一个文件选择框

OpenFileDialog ofd = new OpenFileDialog();

ofdTitle = "Excel文件";

ofdFileName = "";

ofdFilter = "Excel文件(xls)|";

try

{

if (ofdShowDialog() == DialogResultOK)

{

string tableName = "";

//if (arratlist != null)

//{

// arratlistClear();

//}

string Path = ofdFileName;

string strConn = "Provider=MicrosoftAceOleDb120;" + "data source=" + Path + ";Extended Properties='Excel 120; HDR=Yes; IMEX=1'";

OleDbConnection conn = new OleDbConnection(strConn);

connOpen();

SystemDataDataTable schemaTable = connGetOleDbSchemaTable(SystemDataOleDbOleDbSchemaGuidTables, null);

tableName = schemaTableRows[0][2]ToString()Trim();

//for (int i = 0; i < schemaTableRowsCount; i++)

//{

// arratlistAdd(schemaTableRows[i][2]ToString()TrimStart('/'')Trim('/'', '$'));//

//}

string strExcel = "Select From [" + tableName + "]";

OleDbCommand cmd = new OleDbCommand(strExcel, conn);

DataTable excelDt = new DataTable();

OleDbDataAdapter da = new OleDbDataAdapter(strExcel, conn);

daFill(excelDt);

connClose();

MessageBoxShow("excel 导入成功", "提示", MessageBoxButtonsOK, MessageBoxIconInformation);

if (excelDt != null)

{

dataGridView1DataSource = excelDt;

}

}

}

catch (Exception ex)

{

MessageBoxShow("导入文件时出错,文件可能正被打开", "提示");

}

}

类 BLL10101 的代码还不完整,没法判断。也就是你代码的最后一句,用了BLL10101的InsertOrUpdate方法,但是这个方法具体是怎么写的,你的代码没有贴出来。另外,他的源代码要求数据的格式为:(xmlx,xmbm,xmmc,xmdw,dydj,zyxf,xmnr,kssj,jssj) ,你要看看你自己的数据是不是这个格式。

以上就是关于VB.NET将datagridview的数据插入数据库全部的内容,包括:VB.NET将datagridview的数据插入数据库、如何把datagridview控件中一行数据导入SQLSERVER数据库中、导入excel把数据存在datatable中,用sqlbulkcopy把datable批量导入到数据库的表中等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9656402.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存