是数据库教程连接字符串,必要的如下:
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();
}
我最强舞者 (I, Best Dancer)安卓版v8
我最强舞者(I, Best Dancer)是一款休闲放置类手
迷你世界国服版本2024 v1.43.0
迷你世界国服版本2024是一款自由度非常高的沙盒游戏,玩法和
烹饪乐园 安卓版v1.23.6
烹饪乐园(Cooking Town)是一款非常好玩的餐厅模拟
迷你世界小米服 最新安卓版v1.43.0
迷你世界小米版是由迷你玩科技开发的休闲模拟经营类游戏。此版本
布娃娃Sprunki沙盒 安卓版v0.0.1
布娃娃Sprunki沙盒是一个非常有趣的沙盒游戏,复古简约的