C#winform连接数据库
来源:互联网 发布:支付宝怎么改淘宝密码 编辑:程序博客网 时间:2024/05/16 06:43
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace register
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void login_Click(object sender, EventArgs e)
{
string constr = "data source=Z0IFVEMLZUDERWD//数据源;initial catalog=testDB//数据库名;user id=myuser//用户名;pwd=a";
SqlConnection con = new SqlConnection(constr);//开始连接数据库
// con.ConnectionString = constr;
string sql = "select count(*) from student";
SqlCommand com = new SqlCommand(sql, con);//执行数据库语句
try
{
con.Open();
MessageBox.Show("成功连接数据库");
int x = (int)com.ExecuteScalar();
MessageBox.Show(string.Format("成功读取{0},条记录", x));
}
catch (Exception)
{
throw;
}
finally {
con.Close();
MessageBox.Show("成功关闭数据库连接", "提示信息", MessageBoxButtons.YesNoCancel);
}
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace register
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void login_Click(object sender, EventArgs e)
{
string constr = "data source=Z0IFVEMLZUDERWD//数据源;initial catalog=testDB//数据库名;user id=myuser//用户名;pwd=a";
SqlConnection con = new SqlConnection(constr);//开始连接数据库
// con.ConnectionString = constr;
string sql = "select count(*) from student";
SqlCommand com = new SqlCommand(sql, con);//执行数据库语句
try
{
con.Open();
MessageBox.Show("成功连接数据库");
int x = (int)com.ExecuteScalar();
MessageBox.Show(string.Format("成功读取{0},条记录", x));
}
catch (Exception)
{
throw;
}
finally {
con.Close();
MessageBox.Show("成功关闭数据库连接", "提示信息", MessageBoxButtons.YesNoCancel);
}
}
}
}
0 0
- C#winform连接数据库
- C#Winform连接Oracle数据库
- Winform连接远程数据库
- winform 加密连接数据库
- C#winform直接连接SQL数据库mdf文件
- C#winform直接连接SQL数据库mdf文件
- C#winform应用程序连接sql server数据库代码
- C#Winform连接摄像头
- Winform连接access数据库操作
- Winform连接access数据库操作
- C#winform打包安装数据库
- C#winform dataGridView更新数据库
- C#winform dataGridView更新数据库
- C#winform treeview连接数据库后自动识别主键并显示出来
- C#WinForm连接本地或远程Sql数据库,并进行简单操作。
- WinForm中使用配置文件连接数据库
- c#2005中winform如何连接数据库
- c#2005中winform如何连接数据库
- eclipse/ADT 安装Gradle
- ORCAD层次化设计遇到Cannot descend from a schematic page that is not contained within the hierarchy
- 电子或通信领域当前的主流技术及其社会需求调查报告
- 实现多线程的两种方式
- JDBC获取数据库大字段
- C#winform连接数据库
- Assertion failure in -[MASViewConstraint install]
- 考上好大学,然后进入IT行业是穷人孩子晋级中产的唯一出路?(好多年不转载别人的文章了,本文的话题是我非常关注的,一直没有写)
- ubuntu14.04 硬盘迁移记录 HHD->SSD
- [Cloud Networking Notes] Week4
- git客户端连接ssh端口不是22的gitlab
- 全局变量堆内存申请顺序
- MySQL索引和查询优化
- shell的字符串以及字符串的处理