扫描二维码关注

首页 APP开发小程序开发 微信公众号 网站建设 推广运营 关于我们

“学习不仅是掌握知识”

向书本学习,还要向实践学习、向生活学习。消化已有知识,
而且要力求有所发现、有所发明、有所创造

.NET数据库操作

2019/3/24 8:18:11

.NET数据库操作

连接数据库
源代码:


private void Page_Load(object sender, System.EventArgs e)
{

myConnection = new SqlConnection("server=localhost;" +
"database=zhishi;uid=scm;pwd=scm");

//if (!IsPostBack) BindGrid();
}


查询数据库

源代码:

public void BindGrid()
{

myConnection = new SqlConnection("server=localhost;" +
"database=zhishi;uid=scm;pwd=scm");

SqlDataAdapter myCommand = new SqlDataAdapter("SELECT top 10 * FROM" +
" wujml", myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds);

MyDataGrid.DataSource=ds;
MyDataGrid.DataBind();

}


增删改数据库

源代码:

 

public void AddAuthor_Click()
{


String insertCmd = "insert into wujml(wujid,wujbt,wujlj1) "+
"values ('99999','111','3131')";

SqlCommand myCommand = new SqlCommand(insertCmd, myConnection);
myCommand.Connection.Open();

try
{
myCommand.ExecuteNonQuery();
//Message.InnerHtml = "Record Added
" + insertCmd;

}

catch
{
//Console.WriteLine("{0} Second exception caught.", e);

this.body.Text = "*************";

}

myCommand.Connection.Close();


}

连接SQL Server数据库

源代码:

 

//作用: 连接SQL Server数据库

//参数:ServerName 服务器名,DBName 数据库名,UserID 用户名,UserPwd 用户密码

public void ConnSQLServer(string ServerName, string DBName)

{

mCnnDB.ConnectionString = "uid=;pwd= ;driver={SQL Server};" +

"server=" +ServerName+

";database=" + DBName;

mCnnDB.Open(mCnnDB.ConnectionString,"sa","",-1);

}

public void ConnSQLServer(string ServerName, string DBName,string UserID,string UserPwd)

{ mCnnDB.ConnectionString = "uid='" + UserID + "';pwd='" + UserPwd + "' ;driver={SQL Server};" +

"server=" + ServerName +

";database=" + DBName;

mCnnDB.Open(mCnnDB.ConnectionString,UserID,UserPwd,-1);;

}

连接ODBC数据库

源代码:

 

// '作用: 连接ODBC数据库

// '参数:dsnName ODBC名,UserID 用户名,UserPwd 用户密码

public void ConnODBC(string dsnName )

{

mCnnDB.Provider = "MSDASQL";

mCnnDB.ConnectionString = "Data Source='" + dsnName + "'";

mCnnDB.Open(mCnnDB.ConnectionString,"","",-1);

}

public void ConnODBC(string dsnName,string UserID, string UserPwd )

{ mCnnDB.Provider = "MSDASQL";

mCnnDB.ConnectionString = "User ID='" +UserID + "';" +

"Password='" + UserPwd + "';" +

"Data Source='"+dsnName+ "'";

mCnnDB.Open(mCnnDB.ConnectionString,UserID,UserPwd,-1 );


 


长沙高新开发区谷苑路186号湖南大学科技园创业大厦429

咨询电话:0731-8225 2399
业务QQ:1020299919
大客户专线:139 7581 8321

友情链接:
Copyright© 2014-2024 湖南省鑫冠计算机系统有限公司 版权所有 ICP备案号:湘ICP备17013418号-1 公安备案号:湘公网安备43019002001544号