如何用c#连接SQLserver
发布网友
发布时间:2022-09-25 00:23
我来回答
共3个回答
热心网友
时间:2023-09-14 22:10
SqlConnection sqlCon = new SqlConnection();
sqlCon.ConnectionString = "Data Source=;Initial Catalog=;User Id=sa;Password=123456;";
sqlCon.Open();
热心网友
时间:2023-09-14 22:10
SqlConnection sqlCon = new SqlConnection();
sqlCon.ConnectionString = "Data Source=;Initial Catalog=;User Id=sa;Password=123456;";
sqlCon.Open();
热心网友
时间:2023-09-14 22:11
SQLSERVER认证模式:
SqlConnection sqlCon = new SqlConnection();
sqlCon.ConnectionString = "Data Source=;Initial Catalog=;User Id=sa;Password=123456;";
sqlCon.Open();
windows认证模式:
Data Source=.;Initial Catalog=数据库;Integrated Security=True
热心网友
时间:2023-09-14 22:11
SQLSERVER认证模式:
SqlConnection sqlCon = new SqlConnection();
sqlCon.ConnectionString = "Data Source=;Initial Catalog=;User Id=sa;Password=123456;";
sqlCon.Open();
windows认证模式:
Data Source=.;Initial Catalog=数据库;Integrated Security=True
热心网友
时间:2023-09-14 22:11
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
namespace MyWindows
{
public partial class Form1 : Form
{
private SqlConnection conn;
private SqlCommand cmd;
private const string connStr = "server=.;uid=sa;pwd=1234;database=数据库名";
public Form1()
{
GetData();
}
private void GetData()
{
DataTable dt = GetTable("select * from 表名");
}
#region 数据库操作
/// <summary>
/// 打开链接
/// </summary>
/// <returns></returns>
private SqlConnection GetConn()
{
if (conn == null) conn = new SqlConnection(connStr);
if (conn.State == ConnectionState.Closed) conn.Open();
else if (conn.State == ConnectionState.Broken)
{
conn.Close();
conn.Open();
}
return conn;
}
/// <summary>
/// 执行增删改查操作
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private int ExecuteNonQuery(string sql)
{
try
{
cmd = new SqlCommand(sql, GetConn());
return cmd.ExecuteNonQuery();
}
catch
{
return 0;
}
finally
{
conn.Close();
}
}
/// <summary>
/// 读数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private SqlDataReader ExecuteReader(string sql)
{
try
{
cmd = new SqlCommand(sql, GetConn());
return cmd.ExecuteReader();
}
catch
{
return null;
}
finally
{
conn.Close();
}
}
/// <summary>
/// 得到该表数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private DataTable GetTable(string sql)
{
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, GetConn());
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
catch
{
return null;
}
finally
{
conn.Close();
}
}
#endregion
}
}
热心网友
时间:2023-09-14 22:11
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;
namespace MyWindows
{
public partial class Form1 : Form
{
private SqlConnection conn;
private SqlCommand cmd;
private const string connStr = "server=.;uid=sa;pwd=1234;database=数据库名";
public Form1()
{
GetData();
}
private void GetData()
{
DataTable dt = GetTable("select * from 表名");
}
#region 数据库操作
/// <summary>
/// 打开链接
/// </summary>
/// <returns></returns>
private SqlConnection GetConn()
{
if (conn == null) conn = new SqlConnection(connStr);
if (conn.State == ConnectionState.Closed) conn.Open();
else if (conn.State == ConnectionState.Broken)
{
conn.Close();
conn.Open();
}
return conn;
}
/// <summary>
/// 执行增删改查操作
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private int ExecuteNonQuery(string sql)
{
try
{
cmd = new SqlCommand(sql, GetConn());
return cmd.ExecuteNonQuery();
}
catch
{
return 0;
}
finally
{
conn.Close();
}
}
/// <summary>
/// 读数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private SqlDataReader ExecuteReader(string sql)
{
try
{
cmd = new SqlCommand(sql, GetConn());
return cmd.ExecuteReader();
}
catch
{
return null;
}
finally
{
conn.Close();
}
}
/// <summary>
/// 得到该表数据
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
private DataTable GetTable(string sql)
{
try
{
SqlDataAdapter da = new SqlDataAdapter(sql, GetConn());
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
catch
{
return null;
}
finally
{
conn.Close();
}
}
#endregion
}
}