C#怎么连接数据库并使用数据库啊?
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
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;
using System.Data.SqlClient;
namespace database
{
public partial class Form1 : Form
{
//定义什么变量?
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
//这里是连接数据库并使用数据库的代码怎么写啊?
}
}
} 展开
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace database
{
public partial class Form1 : Form
{
//定义什么变量?
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
//这里是连接数据库并使用数据库的代码怎么写啊?
}
}
} 展开
4个回答
展开全部
以下是一个C#与数据库操作的通用类完整明扮代码,几乎可以进行所有的操作。不要在代码中出现SQL语句,要用存储过程,这样才能保证数据的安全。
public class DBHelper
{
private static SqlConnection connection;
private static SqlCommand command;
private static SqlDataAdapter da;
private static DataSet ds;
private static string connString = "Data Source=服务器名;Initial Catalog=数据库名;Integrated Security=true";//用windows身份登录
//连接属性:用于实例化连接对象并打开连接对象
public static SqlConnection Connection
{
get
{
if (connection == null)
{
connection = new SqlConnection(connString);
}
if (connection.State==ConnectionState.Broken || connection.State==ConnectionState.Closed)
{
connection.Open();
}
return connection;
}
}
/// <烂好summary>
/// 通过SQL语句从数据库返回单个值
/// </summary>
/// <param name="strSql">SQL语句</param>
/// <returns>返回值:1、0、-1</returns>
public static int GetScalar(string strSql)
{
try
{
command = new SqlCommand(strSql,connection);
connection.Open();
int i = (int)command.ExecuteScalar();
return i;
}
catch (Exception)
{
connection.Close();
return -1;
}
}
/// <summary>
/// 通过存储过程及饥槐铅参数返回单个值
/// </summary>
/// <param name="storedProcedureName">存储过程名</param>
/// <param name="parameters">参数集合</param>
/// <returns>返回单个值</returns>
public static object GetScalar(string storedProcedureName, params SqlParameter[] parameters)
{
try
{
//将存储过程封装在命令对象
command = new SqlCommand(storedProcedureName, Connection);
//指定命令对象执行的类型为存储过程
command.CommandType = CommandType.StoredProcedure;
//将参数添加到命令对象的参数集合中
command.Parameters.AddRange(parameters);
//执行命令
object o = command.ExecuteScalar();
//关闭连接
connection.Close();
//返回对象
return o;
}
catch (Exception)
{
connection.Close();
return null;
}
}
/// <summary>
/// 通过存储过程及参数查询返回数据集
/// </summary>
/// <param name="storedProcedureName">存储过程名</param>
/// <param name="parameters">参数集合</param>
/// <returns>返回数据集</returns>
public static DataSet GetDataSet(string storedProcedureName, params SqlParameter[] parameters)
{
//将存储过程封装在命令对象
command = new SqlCommand(storedProcedureName, Connection);
//指定命令执行的类型为存储过程
command.CommandType = CommandType.StoredProcedure;
//将参数添加到命令对象的参数集合中
command.Parameters.AddRange(parameters);
da = new SqlDataAdapter(command);
ds = new DataSet();
da.Fill(ds);
return ds;
}
/// <summary>
/// 通过存储过程及参数执行对表的增删改操作
/// </summary>
/// <param name="storedProcedureName">存储过程名</param>
/// <param name="parameters">参数集合</param>
/// <returns>返回操作是否成功</returns>
public static bool Execute(string storedProcedureName,params SqlParameter[] parameters)
{
try
{
command = new SqlCommand(storedProcedureName, Connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddRange(parameters);
command.ExecuteNonQuery();
connection.Close();
return true;
}
catch (Exception)
{
connection.Close();
return false;
}
}
}
public class DBHelper
{
private static SqlConnection connection;
private static SqlCommand command;
private static SqlDataAdapter da;
private static DataSet ds;
private static string connString = "Data Source=服务器名;Initial Catalog=数据库名;Integrated Security=true";//用windows身份登录
//连接属性:用于实例化连接对象并打开连接对象
public static SqlConnection Connection
{
get
{
if (connection == null)
{
connection = new SqlConnection(connString);
}
if (connection.State==ConnectionState.Broken || connection.State==ConnectionState.Closed)
{
connection.Open();
}
return connection;
}
}
/// <烂好summary>
/// 通过SQL语句从数据库返回单个值
/// </summary>
/// <param name="strSql">SQL语句</param>
/// <returns>返回值:1、0、-1</returns>
public static int GetScalar(string strSql)
{
try
{
command = new SqlCommand(strSql,connection);
connection.Open();
int i = (int)command.ExecuteScalar();
return i;
}
catch (Exception)
{
connection.Close();
return -1;
}
}
/// <summary>
/// 通过存储过程及饥槐铅参数返回单个值
/// </summary>
/// <param name="storedProcedureName">存储过程名</param>
/// <param name="parameters">参数集合</param>
/// <returns>返回单个值</returns>
public static object GetScalar(string storedProcedureName, params SqlParameter[] parameters)
{
try
{
//将存储过程封装在命令对象
command = new SqlCommand(storedProcedureName, Connection);
//指定命令对象执行的类型为存储过程
command.CommandType = CommandType.StoredProcedure;
//将参数添加到命令对象的参数集合中
command.Parameters.AddRange(parameters);
//执行命令
object o = command.ExecuteScalar();
//关闭连接
connection.Close();
//返回对象
return o;
}
catch (Exception)
{
connection.Close();
return null;
}
}
/// <summary>
/// 通过存储过程及参数查询返回数据集
/// </summary>
/// <param name="storedProcedureName">存储过程名</param>
/// <param name="parameters">参数集合</param>
/// <returns>返回数据集</returns>
public static DataSet GetDataSet(string storedProcedureName, params SqlParameter[] parameters)
{
//将存储过程封装在命令对象
command = new SqlCommand(storedProcedureName, Connection);
//指定命令执行的类型为存储过程
command.CommandType = CommandType.StoredProcedure;
//将参数添加到命令对象的参数集合中
command.Parameters.AddRange(parameters);
da = new SqlDataAdapter(command);
ds = new DataSet();
da.Fill(ds);
return ds;
}
/// <summary>
/// 通过存储过程及参数执行对表的增删改操作
/// </summary>
/// <param name="storedProcedureName">存储过程名</param>
/// <param name="parameters">参数集合</param>
/// <returns>返回操作是否成功</returns>
public static bool Execute(string storedProcedureName,params SqlParameter[] parameters)
{
try
{
command = new SqlCommand(storedProcedureName, Connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddRange(parameters);
command.ExecuteNonQuery();
connection.Close();
return true;
}
catch (Exception)
{
connection.Close();
return false;
}
}
}
展开全部
string connstr = "Data Source=(local); database=数据库名; user id=登录橘薯禅名; password=密圆尘码";
string sql = "select * from char_test";//sql语手冲句
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
try
{
SqlCommand comm = new SqlCommand(sql, conn);
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
TextBox2.Text = reader[0].ToString();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
string sql = "select * from char_test";//sql语手冲句
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
try
{
SqlCommand comm = new SqlCommand(sql, conn);
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
TextBox2.Text = reader[0].ToString();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先说你要连哪种数据库啊?
连接不同的数据库是不一样的
连接不同的数据库是不一样的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
string sql = "select * from char_test";//sql语句
SqlConnection conn = new SqlConnection(connstr);
using(conn){
conn.Open();
try
{
SqlCommand comm = new SqlCommand(sql, conn);
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
TextBox2.Text = reader[0].ToString();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
SqlConnection conn = new SqlConnection(connstr);
using(conn){
conn.Open();
try
{
SqlCommand comm = new SqlCommand(sql, conn);
SqlDataReader reader = comm.ExecuteReader();
if (reader.Read())
TextBox2.Text = reader[0].ToString();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询