最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net SqlConnection连接数据库详解
时间:2022-06-25 06:04:25 编辑:袖梨 来源:一聚教程网
是数据库教程连接字符串,必要的如下:
New SqlConnection( "Server=LocalHost;DataBase=Pubs;UID=sa;PWD=123 ")
下面先看个实例
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=(local);Initial Catalog=news;User ID=sa;Password=123";SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;for (int i=0;i<=GridView1.Rows.Count-1;i++)
{
CheckBox cbox=(CheckBox)GridView1.Rows [i].FindControl ("CheckBox1");
if (cbox.Checked == true)
{
cmd.CommandText = "update 主题表 set 审核='已审核' where 主帖编号="+GridView1 .DataKeys[i].Value;
conn.Open();
cmd.ExecuteReader();
conn.Close();
}
}
我们对上面实例一句句解析说明
1、建立一个数据库连接对象 conn
2、指定欲连接的数据库参数:服务器为本地服务器,数据库名'news’,用户ID为'sa',密码为'123'
3、建立一个Command对象 cmd
4、将cmd的Connection变量赋值为第1步建立的对象conn
5、for循环遍历GridView1控件里的每一行
6、获得GridView1第i行里的‘CheckBox1’属性
7、判断CheckBox是否选中
8、为cmd指定执行内容,更新数据库‘主题表’里‘主帖编号’="+GridView1 .DataKeys[i].Value的那一行里‘审核’字段内容为‘已审核’
9、连接数据库
10、执行第8步指定的内容
11、关闭数据库连接
好了综合上述我们写个完整的实例
<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Data.OleDb" %>
<%@ import Namespace="System.Data.Odbc" %>
void Page_Load(object sender, EventArgs e) {
SqlConnection mySqlConnection = new SqlConnection("Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=java2suser;Password='password';");mySqlConnection.Open();
lblSqlConnectionString.Text = mySqlConnection.ConnectionString;
lblSqlConnectionTimeout.Text = Convert.ToString(mySqlConnection.ConnectionTimeout);
lblSqlDatabase.Text = mySqlConnection.Database;
lblSqlDataSource.Text = mySqlConnection.DataSource;
lblSqlPacketSize.Text = Convert.ToString(mySqlConnection.PacketSize);
lblSqlServerVersion.Text = mySqlConnection.ServerVersion;
lblSqlState.Text = Convert.ToString(mySqlConnection.State);
lblSqlWorkstationId.Text = mySqlConnection.WorkstationId;
// close the connection
mySqlConnection.Close();
// *******************
// * OleDbConnection *
// *******************
// create the object
OleDbConnection myOleDbConnection = new OleDbConnection("provider=sqloledb;server=(local)BAND;database=Books;uid=band;pwd=letmein;");
// open the connection
myOleDbConnection.Open();
// set the labels on the form
lblOleDbConnectionString.Text = myOleDbConnection.ConnectionString;
lblOleDbConnectionTimeout.Text = Convert.ToString(myOleDbConnection.ConnectionTimeout);
lblOleDbDatabase.Text = myOleDbConnection.Database;
lblOleDbDataSource.Text = myOleDbConnection.DataSource;
lblOleDbProvider.Text = myOleDbConnection.Provider;
lblOleDbServerVersion.Text = myOleDbConnection.ServerVersion;
lblOleDbState.Text = Convert.ToString(myOleDbConnection.State);
// close the connection
myOleDbConnection.Close();
// *******************
// * OdbcConnection *
// *******************
// create the object
OdbcConnection myOdbcConnection = new OdbcConnection("driver={SQL Server};server=(local)BAND;database=Books;uid=band;pwd=letmein;");
// open the connection
myOdbcConnection.Open();
// set the labels on the form
lblOdbcConnectionString.Text = myOdbcConnection.ConnectionString;
lblOdbcConnectionTimeout.Text = Convert.ToString(myOdbcConnection.ConnectionTimeout);
lblOdbcDatabase.Text = myOdbcConnection.Database;
lblOdbcDataSource.Text = myOdbcConnection.DataSource;
lblOdbcDriver.Text = myOdbcConnection.Driver;
lblOdbcServerVersion.Text = myOdbcConnection.ServerVersion;
lblOdbcState.Text = Convert.ToString(myOdbcConnection.State);
// close the connection
myOdbcConnection.Close();
}
相关文章
- 江南百景图碎金泉怎么样 12-26
- 江南百景图游宴廊怎么样 江南百景图游宴廊建筑介绍 12-26
- 江南百景图碎金泉怎么样 江南百景图碎金泉建筑介绍 12-26
- 炉石传说兑换码大全 12-26
- 重返未来1999趋光性研究夜幕之外怎么玩 趋光性夜幕之外活动介绍 12-26
- 光遇12.26大蜡烛在哪里 光遇12月26日大蜡烛位置攻略 12-26