4个回答
展开全部
说白了就是怎么用C#操作数据库吧。
C#操纵数据库使用ADO+组件,命名空间为System.Data;以及它下面的几个子空间,分别用来处理Ole数据库,ODBC数据库,Sql Server数据库,以及其他数据库等。
我用Access数据库来说明:
1。首先引入命名空间
2。申明连接变量:
OleDbConnection OleCon=new OleDbConnection(Provider=Microsoft.ACE.OLEDB.12.0;Data Source=db.mdb);
3。申明一个执行能SQL命令SQLCommand:
OleDbCommand OleCom=OleDbCommand();
4。将连接与SQLCommand项关联:
OleCom.Connection = OleCon;
5。给SQLCommand赋SQL语句:
OleCom.CommandText = "select * from biao";
6。执行SQLCommand:
执行的方法很多,有返回DataReader的,有返回受影响行数的,有返回结果第一行第一列的,还有使用适配器填充DataSet的。请看下面我写的一个类吧:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
namespace MyWebSite.DataControl
{
public class AccessControl
{
private OleDbConnection OleCon;
private OleDbCommand OleCom;
public AccessControl()
{
OleCon = new OleDbConnection(ConfigurationSettings.AppSettings["ConStr"].ToString());
OleCom = new OleDbCommand();
OleCom.Connection = OleCon;
}
/// <summary>
/// 返回执行所受影响的函数
/// </summary>
/// <param name="com"></param>
/// <returns></returns>
public int ExecuteGetLines(string com)
{
try
{
OleCom.CommandText = com;
OleCom.Connection = OleCon;
OleCon.Open();
int i = 0;
i = OleCom.ExecuteNonQuery();
return i;
}
catch(Exception e)
{
throw new System.ArgumentException("错误",e.Message);
}
finally
{
CloseCon();
}
}
/// <summary>
/// 返回结果的第一行第一列
/// </summary>
/// <param name="com"></param>
/// <returns></returns>
public object ExecuteGetCol(string com)
{
try
{
OleCom.CommandText = com;
OleCom.Connection = OleCon;
OleCon.Open();
object o = null;
o = (object)OleCom.ExecuteScalar();
return o;
}
catch (Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
finally
{
CloseCon();
}
}
/// <summary>
/// 返回一个数据集合DataTable
/// </summary>
/// <param name="com"></param>
/// <returns></returns>
public DataTable ExecuteGetTable(string com)
{
try
{
DataTable dt = new DataTable();
OleCom.CommandText = com;
OleCom.Connection = OleCon;
OleDbDataAdapter dad = new OleDbDataAdapter(OleCom);
dad.Fill(dt);
return dt;
}
catch(Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
finally
{
CloseCon();
}
}
/// <summary>
/// 关闭连接
/// </summary>
public void CloseCon()
{
OleCom.Dispose();
OleCon.Close();
}
}
}
C#操纵数据库使用ADO+组件,命名空间为System.Data;以及它下面的几个子空间,分别用来处理Ole数据库,ODBC数据库,Sql Server数据库,以及其他数据库等。
我用Access数据库来说明:
1。首先引入命名空间
2。申明连接变量:
OleDbConnection OleCon=new OleDbConnection(Provider=Microsoft.ACE.OLEDB.12.0;Data Source=db.mdb);
3。申明一个执行能SQL命令SQLCommand:
OleDbCommand OleCom=OleDbCommand();
4。将连接与SQLCommand项关联:
OleCom.Connection = OleCon;
5。给SQLCommand赋SQL语句:
OleCom.CommandText = "select * from biao";
6。执行SQLCommand:
执行的方法很多,有返回DataReader的,有返回受影响行数的,有返回结果第一行第一列的,还有使用适配器填充DataSet的。请看下面我写的一个类吧:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
namespace MyWebSite.DataControl
{
public class AccessControl
{
private OleDbConnection OleCon;
private OleDbCommand OleCom;
public AccessControl()
{
OleCon = new OleDbConnection(ConfigurationSettings.AppSettings["ConStr"].ToString());
OleCom = new OleDbCommand();
OleCom.Connection = OleCon;
}
/// <summary>
/// 返回执行所受影响的函数
/// </summary>
/// <param name="com"></param>
/// <returns></returns>
public int ExecuteGetLines(string com)
{
try
{
OleCom.CommandText = com;
OleCom.Connection = OleCon;
OleCon.Open();
int i = 0;
i = OleCom.ExecuteNonQuery();
return i;
}
catch(Exception e)
{
throw new System.ArgumentException("错误",e.Message);
}
finally
{
CloseCon();
}
}
/// <summary>
/// 返回结果的第一行第一列
/// </summary>
/// <param name="com"></param>
/// <returns></returns>
public object ExecuteGetCol(string com)
{
try
{
OleCom.CommandText = com;
OleCom.Connection = OleCon;
OleCon.Open();
object o = null;
o = (object)OleCom.ExecuteScalar();
return o;
}
catch (Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
finally
{
CloseCon();
}
}
/// <summary>
/// 返回一个数据集合DataTable
/// </summary>
/// <param name="com"></param>
/// <returns></returns>
public DataTable ExecuteGetTable(string com)
{
try
{
DataTable dt = new DataTable();
OleCom.CommandText = com;
OleCom.Connection = OleCon;
OleDbDataAdapter dad = new OleDbDataAdapter(OleCom);
dad.Fill(dt);
return dt;
}
catch(Exception e)
{
throw new System.ArgumentException("错误", e.Message);
}
finally
{
CloseCon();
}
}
/// <summary>
/// 关闭连接
/// </summary>
public void CloseCon()
{
OleCom.Dispose();
OleCon.Close();
}
}
}
展开全部
给你个参考代码,你看看吧。 string Computername = System.Net.Dns.GetHostName();
string connectSql = "Data Source=" + Computername + ";Initial Catalog=WebBank//要连接的数据库;Integrated Security=True";
SqlConnection sqlConnection = new SqlConnection(connectSql);
sqlConnection.Open();
string sqlCommand1 = "select Password from AgricultureBank where cardId=" + Convert.ToInt32(textBox3.Text);
//查询语句
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand1, sqlConnection);
DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds);
string password = ds.Tables[0].Rows[0]["password"].ToString();
this.textBox1.Text=password;
sqlConnection.Close();
把密码从数据库里拿出后赋值给textBox1.Text
string connectSql = "Data Source=" + Computername + ";Initial Catalog=WebBank//要连接的数据库;Integrated Security=True";
SqlConnection sqlConnection = new SqlConnection(connectSql);
sqlConnection.Open();
string sqlCommand1 = "select Password from AgricultureBank where cardId=" + Convert.ToInt32(textBox3.Text);
//查询语句
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand1, sqlConnection);
DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds);
string password = ds.Tables[0].Rows[0]["password"].ToString();
this.textBox1.Text=password;
sqlConnection.Close();
把密码从数据库里拿出后赋值给textBox1.Text
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你先把结果查询出来,然后绑定给一个dataset取名 ds
this.txtvalue.Text=ds.Tables[0].Row[0]["字段名"].ToString();
this.txtvalue.Text=ds.Tables[0].Row[0]["字段名"].ToString();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询