数据无法在数据库中更新,但在datagridview c#windows应用程序中更新 [英] data cannot update in database but update in datagridview c# windows application

查看:64
本文介绍了数据无法在数据库中更新,但在datagridview c#windows应用程序中更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



正在使用c#.net开发Windows应用程序,这里我需要在datagridview中更新和删除,实际上我完成了上面的那个是在我身边更新和删除了。

但是数据无法在数据库中更新,但在datagridview中以同样的方式更新也有删除选项,所以请在下面找出我的代码我错误的地方为什么不在数据库中更新。请给我一个解决方案。



提前感谢...



我的整个代码是:

 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Text;
使用 System.Windows.Forms;
使用 System.Data.SqlClient;
使用 System.Configuration;


命名空间 tarriff_design_page
{
public 部分 测试:表格
{

// string s = @Data Source = .\SQLEXPRESS; Initial Catalog = tarrif; User ID = sa; Password = sa ;



private SqlDataAdapter da;
private SqlConnection conn;
BindingSource bsource = new BindingSource();
DataSet ds = null ;
string sql;


public testing()
{
InitializeComponent();

LoadData();

}
public static 字符串 hh
{
获取
{

}
}



private void btnupdate_Click( object sender,EventArgs e)
{

try
{
DialogResult result = MessageBox.Show( 您确定要继续更新吗? 更新确认,MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
SqlConnection CN = new SqlConnection( @ Data Source = .\SQLEXPRESS; Initial Catalog = tarrif; User ID = sa;密码= SA);
CN.Open();

string query = 选择* from test;

SqlDataAdapter dAdapter = new SqlDataAdapter(query,CN);

SqlCommandBuilder cBuilder = new SqlCommandBuilder(dAdapter);

DataTable dTable = new DataTable();

dAdapter.Fill(dTable);

DataGridView DGview = new DataGridView();

BindingSource bSource = new BindingSource();

DGview.DataSource = bSource;

dAdapter.Update(dTable);

CN.Close();

}
其他
{

}

}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}

}

private void btndelete_Click( object sender,EventArgs e)
{
try
{
DialogResult result = MessageBox.Show( 您确定要删除此行吗? 删除确认,MessageBoxButtons.YesNo,MessageBoxIcon.Question);

if (result == DialogResult.Yes)
{
foreach (DataGridViewRow dr in dataGridView1.SelectedRows)
{
if (dataGridView1.Rows.Count > 1
dataGridView1.Rows.Remove(dr );
}
}
}
catch (异常exceptionObj)
{
MessageBox.Show (exceptionObj.Message.ToString());
}
}



public void LoadData()
{
string connectionString = @ Data Source = .\SQLEXPRESS; Initial Catalog = tarrif; User ID = sa; Password = sa;
conn = new SqlConnection(connectionString);
sql = select * from test;
da = new SqlDataAdapter(sql,conn);
conn.Open();
ds = new DataSet();
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da);
da.Fill(ds, test);
bsource.DataSource = ds.Tables [ test];
dataGridView1.DataSource = bsource;
}

private void testing_Load( object sender,EventArgs e)
{
LoadData();
}
}
}

解决方案





如果要将数据更新到DB中,则应在btndelete_Click事件中使用Update语句。



SqlConnection CN = new SqlConnection(@Data Source = .\SQLEXPRESS; Initial Catalog = tarrif; User ID = sa; Password = sa);

CN.Open();



string query =更新表''表名''设置''column_name''=''值''其中''某些条件'';



SqlCommand Cmd = new SqlCommand(query,CN);

Cmd.Connection = CN;

Cmd.ExecuteNonQuery();

CN .Close();

Hi every one,

am developing windows application using c#.net, here i need to update and delete in datagridview, actually i done the above one that is which is updated and deleted in my side.
but the thing is the data cannot update in database but update in datagridview the same way has also deleting option, so please find out my code below where i did mistake why am not updating in database. please give me a solution.

thanks in advance...

my entire code is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;


namespace tarriff_design_page
{
    public partial class testing : Form
    {
        
        // string s = @"Data Source=.\SQLEXPRESS;Initial Catalog=tarrif;User ID=sa;Password=sa";

        

        private SqlDataAdapter da;
        private SqlConnection conn;
        BindingSource bsource = new BindingSource();
        DataSet ds = null;
        string sql;


        public testing()
        {
            InitializeComponent();
          
            LoadData();
            
        }
        public static String hh
        {
            get
            {
                
            }
        }

       

        private void btnupdate_Click(object sender, EventArgs e)
        {
           
            try
{
DialogResult result = MessageBox.Show("Are you sure you want to proceed Updation ?", "Update Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
SqlConnection CN = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=tarrif;User ID=sa;Password=sa");
CN.Open();

string query = "Select * from test";

SqlDataAdapter dAdapter = new SqlDataAdapter(query, CN);

SqlCommandBuilder cBuilder = new SqlCommandBuilder(dAdapter);

DataTable dTable = new DataTable();

dAdapter.Fill(dTable);

DataGridView DGview = new DataGridView();

BindingSource bSource = new BindingSource();

DGview.DataSource = bSource;

dAdapter.Update(dTable);

CN.Close();

}
else
{

}

}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}

        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            try
{
DialogResult result = MessageBox.Show("Are you sure you want to delete this row?", "Delete confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

if (result == DialogResult.Yes)
{
foreach (DataGridViewRow dr in dataGridView1.SelectedRows)
{
if (dataGridView1.Rows.Count > 1)
dataGridView1.Rows.Remove(dr);
}
}
                }
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
        }



        public void LoadData()
        {
            string connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=tarrif;User ID=sa;Password=sa";
            conn = new SqlConnection(connectionString);
            sql = "select * from test";
            da = new SqlDataAdapter(sql, conn);
            conn.Open();
            ds = new DataSet();
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da);
            da.Fill(ds, "test");
            bsource.DataSource = ds.Tables["test"];
            dataGridView1.DataSource=bsource;
        }

        private void testing_Load(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}

解决方案

Hi,

If you want to update data into DB, you should use Update statement in btndelete_Click event.

SqlConnection CN = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=tarrif;User ID=sa;Password=sa");
CN.Open();

string query = "Update Table ''Table Name'' Set ''column_name''=''value'' where ''Some condition''";

SqlCommand Cmd = new SqlCommand(query , CN);
Cmd.Connection = CN;
Cmd.ExecuteNonQuery();
CN.Close();


这篇关于数据无法在数据库中更新,但在datagridview c#windows应用程序中更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆