最新下载
热门教程
- 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();
}
相关文章
- 王者荣耀侦探能力大测试攻略 王者荣耀侦探能力大测试怎么过 11-22
- 无期迷途主线前瞻兑换码是什么 11-22
- 原神欧洛伦怎么培养 11-22
- 炉石传说网易云音乐联动怎么玩 11-22
- 永劫无间手游确幸转盘怎么样 11-22
- 无期迷途主线前瞻兑换码是什么 无期迷途主线前瞻直播兑换码介绍 11-22