C#中SqlConnection()的用法
先创建SqlConnection(string)新的实例对象,再用open()打开Sqlserver数据库,再用Close()方法关闭打开的sqlserver数据库。Windows验证登录:Server=服务器名称;SQL验证登录:Server=服务器名称;user=登录SQL的用户名;pwd=登录SQL的用户名的密码;连接SQL Server数据库的方式一般有两种,一种是以SQL身份验证登录,一
目录
4.连接上图windows验证登录的数据库db_CSharp
C#的数据库编程中,SqlConnection()主要用于连接Sqlserver数据库。
一、建立数据库
C#中SQL server的成功安装-CSDN博客 https://blog.csdn.net/wenchm/article/details/133499909?spm=1001.2014.3001.5501
二、连接数据库
1.数据库登录方式
连接SQL Server数据库的方式一般有两种,一种是以SQL身份验证登录,一种是以Windows身份验证登录。
2.数据库连接格式
两种登录方式的SqlConnection()数据库连接格式是不同的。
SQL验证登录:Server=服务器名称;user=登录SQL的用户名;pwd=登录SQL的用户名的密码;database=数据库名称;
Windows验证登录:Server=服务器名称;integrated security=SSPI;Initial Catalog=数据库名称;
3.SqlConnection()方法打开数据库
先创建SqlConnection(string)新的实例对象,再用open()打开Sqlserver数据库,再用Close()方法关闭打开的sqlserver数据库。
通过try...catch...finally语句控制连接数据库释放资源。
// SQL验证登录:Server=服务器名称;user=登录SQL的用户名;pwd=登录SQL的用户名的密码;database=数据库名称;
// Windows验证登录:Server=服务器名称;integrated security=SSPI;Initial Catalog=数据库名称;
string ConStr = "server=DESKTOP-QFENBNJ\\SQL_WEN;integrated security=SSPI;Initial Catalog=" + textBox1.Text.Trim();
SqlConnection conn = new SqlConnection(ConStr);
conn.Open();
4.连接上图windows验证登录的数据库db_CSharp
(1)源码
//Form1.cs
//连接数据库
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Test01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入要连接的数据库名称");
}
else
{
try
{
// SQL验证登录:Server=服务器名称;user=登录SQL的用户名;pwd=登录SQL的用户名的密码;database=数据库名称;
// Windows验证登录:Server=服务器名称;integrated security=SSPI;Initial Catalog=数据库名称;
string ConStr = "server=DESKTOP-QFENBNJ\\SQL_WEN;integrated security=SSPI;Initial Catalog=" + textBox1.Text.Trim();
SqlConnection conn = new SqlConnection(ConStr);
conn.Open();
if (conn.State == ConnectionState.Open)
{
label2.Text = "数据库【" + textBox1.Text.Trim() + "】已经连接并打开";
}
}
catch
{
MessageBox.Show("连接数据库失败");
}
}
}
}
}
(2)生成效果
三、关闭数据库
当对数据库操作完毕后,要关闭与数据库的连接,释放占用的资源。通过调用SqlConnection对象的 Close方法或Dispose方法关闭与数据库的连接,这两种方法的主要区别是:Close方法用于关闭一个连接;而Dispose方法不仅关闭一个连接,而且还清理连接所占用的资源。当使用Close方法关闭连接后,可以再调用Open方法打开连接,不会产生任何错误;而如果使用Dispose方法关闭连接,就不可以再次直接用Open方法打开连接,必须再次重新初始化连接再打开。
1.源码
//Form1.cs
//关闭数据库Close方法或Dispose方法
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _2
{
public partial class Form1 : Form
{
public SqlConnection conn;
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 初始化Form1
/// </summary>
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Size = new Size(170, 20);
button1.Size = new Size(75,20);
button2.Size = new Size(330,20);
button3.Size = new Size(330,20);
button1.Text = "连接数据库";
button2.Text = "先使用Close方法关闭连接再用Open方法连接数据库";
button3.Text = "先使用Dispose方法关闭连接再用Open方法连接数据库";
label1.Text= "数据库名称:";
}
/// <summary>
/// 连接数据库
/// SQL验证登录:Server=服务器名称;user=登录SQL的用户名;pwd=登录SQL的用户名的密码;database=数据库名称;
/// Windows验证登录:Server=服务器名称;integrated security=SSPI;Initial Catalog=数据库名称;
/// </summary>
private void Button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入要连接的数据库名称");
}
else
{
try
{
string ConStr = "Server=DESKTOP-GFMO83R;integrated security=SSPI;Initial Catalog=" + textBox1.Text.Trim();
conn = new SqlConnection(ConStr);
conn.Open();
if (conn.State == ConnectionState.Open)
{
richTextBox1.Text = "数据库【" + textBox1.Text.Trim() + "】已经连接并打开";
}
}
// 也可以有参数catch(Exception ex)
// richTextBox1.Text =(ex.Message);
catch
{
richTextBox1.Text = "连接数据库失败";
//MessageBox.Show("连接数据库失败");
textBox1.Text = "";
}
}
}
/// <summary>
/// Close方法用于关闭一个连接;
/// 当使用Close方法关闭连接后,可以再调用
/// Open方法打开连接,不会产生任何错误;
/// </summary>
private void Button2_Click(object sender, EventArgs e)
{
try
{
string str = "";
conn.Close();
if (conn.State == ConnectionState.Closed)
{
str = "数据库已经成功关闭\n";
}
conn.Open();
if (conn.State == ConnectionState.Open)
{
str += "数据库已经成功打开\n";
}
richTextBox1.Text = str;
}
catch (Exception ex)
{
richTextBox1.Text = ex.Message;
}
}
/// <summary>
/// Dispose方法不仅关闭一个连接,而且还清理连接所占用的资源。
/// 如果使用Dispose方法关闭连接,就不可以再次直接用Open方法
/// 打开连接,必须再次重新初始化连接再打开。
/// </summary>
private void Button3_Click(object sender, EventArgs e)
{
try
{
conn.Dispose();
conn.Open();
}
catch (Exception ex)
{
richTextBox1.Text = ex.Message;
}
}
}
}
2.生成效果
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)