我有一个代码,可将数据从excel导入DataGridView
,然后从那里保存到访问数据库中。我认为我的代码不错,但是我不断获得
“数据类型不匹配”
我相信这是因为DataGridView中的空白单元格。有人可以建议其他方法吗?谢谢
private void btn_sal2_Click(object sender, EventArgs e)
{
OleDbConnection cnEMP2 = new OleDbConnection();
cnEMP2.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\SWFAL\Desktop\McDat2019.mdb";
cnEMP2.Open();
using (OleDbCommand cmdEMP2 = new OleDbCommand())
{
foreach (DataGridViewRow EMPRow in this.dgvsal.Rows)
{
for (int i = 0; i <= EMPRow.Cells.Count; i++)
{
if (EMPRow.Cells[i].Value != null || !string.IsNullOrWhiteSpace(Convert.ToString(EMPRow.Cells[i].Value)))
{
cmdEMP2.Connection = cnEMP2;
string qryEMP = "UPDATE Salinity2 set Texture='" + Convert.ToString(EMPRow.Cells[1].Value) + "', EC='" + Convert.ToString(EMPRow.Cells[2].Value) + "', Cl='" + Convert.ToString(EMPRow.Cells[3].Value) + "', NO3N='" + Convert.ToString(EMPRow.Cells[4].Value) + "', pH='" + Convert.ToString(EMPRow.Cells[5].Value) + "', CO3='" + Convert.ToString(EMPRow.Cells[6].Value) + "', HCO3='" + Convert.ToString(EMPRow.Cells[7].Value) + "', Volume='" + Convert.ToString(EMPRow.Cells[8].Value) + "', [NH4-N]='" + Convert.ToString(EMPRow.Cells[9].Value) + "', Na='" + Convert.ToString(EMPRow.Cells[10].Value) + "', Ca='" + Convert.ToString(EMPRow.Cells[11].Value) + "', Mg='" + Convert.ToString(EMPRow.Cells[12].Value) + "', K='" + Convert.ToString(EMPRow.Cells[13].Value) + "', SO4='" + Convert.ToString(EMPRow.Cells[14].Value) + "', Boron='" + Convert.ToString(EMPRow.Cells[15].Value) + "', [ICAP-P]='" + Convert.ToString(EMPRow.Cells[16].Value) + "', Fe='" + Convert.ToString(EMPRow.Cells[17].Value) + "', Zn='" + Convert.ToString(EMPRow.Cells[18].Value) + "', Cu='" + Convert.ToString(EMPRow.Cells[19].Value) + "', Mn='" + Convert.ToString(EMPRow.Cells[20].Value) + "' where LabID=" + Convert.ToInt32(EMPRow.Cells[0].Value) + " ";
cmdEMP2.CommandText = qryEMP;
cmdEMP2.CommandType = CommandType.Text;
cmdEMP2.ExecuteNonQuery();
}
else
{
}
}
}
}
答案 0 :(得分:0)
要从Excel加载到数据集中,请尝试以下操作。
using System;
using System.Drawing;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
System.Data.OleDb.OleDbConnection MyConnection ;
System.Data.DataSet DtSet ;
System.Data.OleDb.OleDbDataAdapter MyCommand ;
MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\\csharp.net-informations.xls';Extended Properties=Excel 8.0;");
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
MyCommand.TableMappings.Add("Table", "TestTable");
DtSet = new System.Data.DataSet();
MyCommand.Fill(DtSet);
dataGridView1.DataSource = DtSet.Tables[0];
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString());
}
}
}
}
要从datagridview导出到MS Access。
private void btnExportToAccess_Click(object sender, EventArgs e)
{
Spire.DataExport.Access.AccessExport accessExport = new
Spire.DataExport.Access.AccessExport();
accessExport.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
accessExport.DataTable = this.dataGridView1.DataSource as DataTable;
accessExport.DatabaseName = @"..\..\ToMdb.mdb";
accessExport.TableName = "ExportFromDatatable";
accessExport.SaveToFile();
}