我的datagridview值没有插入数据库,错误是什么? [英] my datagridview values are not being inserted in database,what is the error?
本文介绍了我的datagridview值没有插入数据库,错误是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
// 使用系统;
// 使用System.Collections.Generic;
/ / 使用System.ComponentModel;
// 使用System.Data;
// 使用System.Drawing;
// 使用System.Linq;
// 使用System.Text;
// 使用System.Windows.Forms;
使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.Data.SqlClient;
命名空间 dgvexpected
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection mycon = new SqlConnection( @ < span class =code-string> Data Source = C\Users\Kkavyansh \Desktop\ppt wkr prjct\dgvoperation.accdb; Persist Security Info = True);
private void button1_Click(对象发件人,EventArgs e)
{
尝试
{
string col1 = dataGridView1 [ 0 ,dataGridView1.CurrentCell.RowIndex] .Value.ToString();
string col2 = dataGridView1 [ 1 ,dataGridView1.CurrentCell.RowIndex] .Value.ToString( );
string col3 = dataGridView1 [ 2 ,dataGridView1.CurrentCell.RowIndex] .Value.ToString( );
string col4 = dataGridView1 [ 3 ,dataGridView1.CurrentCell.RowIndex] .Value.ToString( );
string col5 = dataGridView1 [ 4 ,dataGridView1.CurrentCell.RowIndex] .Value.ToString( );
// string col6 = dataGridView1 [5,dataGridView1.CurrentCell.RowIndex] .Value.ToString();
textBox1.Text = col1;
textBox2.Text = col2;
textBox3.Text = col3;
textBox4.Text = col4;
textBox5.Text = col5;
string insert_sql = INSERT INTO dgv(stdid,stdname,gender,phone,address)VALUES(' + col1 +',' + col2 +',' col3',' + col4 +',' + col5 +');
SqlCommand sqlcomm = new SqlCommand(insert_sql,mycon);
mycon.Open();
sqlcomm.ExecuteNonQuery();
// sqlcomm.Dispose();
mycon.Close();
}
catch
{
}
}
私有 void Form1_Load(< span class =code-keyword> object sender,EventArgs e)
{
// TODO:这行代码将数据加载到'dgvoperationDataSet.dgv'表中。您可以根据需要移动或删除它。
此 .dgvTableAdapter.Fill( this 跨度> .dgvoperationDataSet.dgv);
}
}
}
解决方案
你在插入查询中错过了双重报价,
应该是这样的。
< span class =code-keyword> string insert_sql = INSERT INTO dgv(stdid,stdname,gender ,电话,地址)VALUES(' + col1 + ',' + col2 + ',' col3 ',' + col4 + ',' + col5 + ');
您正在使用SqlConnection
来访问Access数据库。SqlConnection
仅适用于sql server数据库。对于连接字符串,最好在网站下方查看。
https://www.connectionstrings.com/access/ [ ^ ]
样本应用程序检查以下文章
插入,更新,在ASP.NET Gridview中删除,将DataSource删除为SQL Server,MS Access(mdb / accdb),XML和Framework为2.0 / 3.0 / 3.5 / 4.0(VS 2005/2008/2010) [ ^ ]
//using System;
//using System.Collections.Generic;
//using System.ComponentModel;
//using System.Data;
//using System.Drawing;
//using System.Linq;
//using System.Text;
//using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace dgvexpected
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection mycon = new SqlConnection(@"Data Source=C\Users\Kkavyansh\Desktop\ppt wkr prjct\dgvoperation.accdb;Persist Security Info=True");
private void button1_Click(object sender, EventArgs e)
{
try
{
string col1 = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
string col2 = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
string col3 = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
string col4 = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
string col5 = dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value.ToString();
//string col6 = dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value.ToString();
textBox1.Text = col1;
textBox2.Text = col2;
textBox3.Text = col3;
textBox4.Text = col4;
textBox5.Text = col5;
string insert_sql = INSERT INTO dgv(stdid,stdname,gender,phone,address)VALUES('" + col1 +"','"+col2+"','"col3"','"+col4+"','"+col5+"' );
SqlCommand sqlcomm = new SqlCommand(insert_sql, mycon);
mycon.Open();
sqlcomm.ExecuteNonQuery();
//sqlcomm.Dispose();
mycon.Close();
}
catch
{
}
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'dgvoperationDataSet.dgv' table. You can move, or remove it, as needed.
this.dgvTableAdapter.Fill(this.dgvoperationDataSet.dgv);
}
}
}
解决方案
you have missed double quote in youe insert query ,
it should be like that.
string insert_sql = "INSERT INTO dgv(stdid,stdname,gender,phone,address)VALUES('" + col1 +"','"+col2+"','"col3"','"+col4+"','"+col5+"' )";
You are usingSqlConnection
to for Access Database.SqlConnection
only work for sql server databases. for the connection strings you better check below site.
https://www.connectionstrings.com/access/[^]
for sample application check below article
Insert, Update, Delete in ASP.NET Gridview, DataSource as SQL Server, MS Access (mdb/accdb), XML and Framework as 2.0 / 3.0 / 3.5 / 4.0 (VS 2005/2008/2010)[^]
这篇关于我的datagridview值没有插入数据库,错误是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文