我相信很多人在开发过程中需要实时更新数据,代码方面没有错误,数据确实添加到了数据库当中,但是数据控件(gridview)无法实时更新我的数据,这是个很头疼的事情,我把解决方法显示如下:
在这里我创建了两个窗体(Form1和Form2)和一个类login.cs并且在Form1窗体里放了一个数据控件(gridview)和timer控件
Form2里放了几个标签控件和文本框控件和一个添加按钮。
像这种跨窗体间的数据实时更新会用到timer控件所以提前添加好。请看上图,下面上代码:
这是login下的打开数据库和关闭数据库,还写了两个操作数据库的方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace 博客2
{
class login
{
//创建连接对象
private SqlConnection conn = null;
//创建连接所需要的信息
private string ConnStr = "server = .;database = Library;uid = sa;pwd = wwk";//我的数据库是用sa进行登录的,密码是wwk
//做一个打开设置
public bool Openconnection()
{
if (conn == null)
{
conn = new SqlConnection(ConnStr);
}
try
{
conn.Open();
return true;
}
catch (Exception)
{
return false;
}
}
//做一个关闭设置
public void CloseConnection()
{
conn.Close();
conn.Dispose();
}
//对数据库进行操作
//查询数据库
SqlCommand sc = null;
public SqlDataReader show(string sqlQuery)
{
sc = new SqlCommand(sqlQuery, conn);
SqlDataReader sdr = sc.ExecuteReader();
return sdr;
}
//增加修改删除数据库
public int executeNonquery(string sqlQuery)
{
sc = new SqlCommand(sqlQuery, conn);
int number = sc.ExecuteNonQuery();
return number;
}
}
}
然后我们在Form2窗体里进行添加到数据库的操作,要先导入命名空间(using Systems.Data.sqlclient;)才可以操作数据库,这里要注意的是打开了数据库,执行了数据库操作后要关闭数据库,以免占用内存 请看代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 博客2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void simpleButton1_Click(object sender, EventArgs e)
{
login lg = new login();
if (lg.Openconnection()) {
int number= lg.executeNonquery("insert into UserK values('" + textEdit1.Text.Trim() + "','" + textEdit2.Text.Trim() + "','" + textEdit3.Text.Trim() + "','" + textEdit4.Text.Trim() + "')");
if (number != 0) { MessageBox.Show("添加成功"); } else { return; }//判断是否添加了数据到数据库
lg.CloseConnection();
}
}
}
}
以上的代码已经成功把数据添加到了数据库 下面我们就要进行实时更新数据库,这是在Form1里面的代码 请看以下代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 博客2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void simpleButton1_Click(object sender, EventArgs e)
{
Form2 fm2 = new Form2();
fm2.ShowDialog();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Start();
login lg = new login();
if (lg.Openconnection()) {
SqlDataReader sdr= lg.show("select * from UserK"); //查询出的数据放在sqldatareader数据集中
gridControl1.DataSource = sdr;//把数据添加到数据控件gridControl里面
lg.CloseConnection();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
login lg = new login();
if (lg.Openconnection())
{
SqlDataReader sdr = lg.show("select * from UserK"); //查询出的数据放在sqldatareader数据集中
gridControl1.DataSource = sdr;//把数据添加到数据控件gridControl里面
lg.CloseConnection();
}
}
}
}
以上所示完成了跨窗体实时更新(本人亲测)!我与各位共同进步学习,加油