?? sqlconnect.aspx
字號:
?<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Connect to SqlServer</title>
<script runat="server">
public void OnConnect(object sender, EventArgs arg)
{
string datasource = "Data Source=" + txtDataSource.Text + ";";
string yanzheng = "user id=" + txtUser.Text + ";password=" + txtPassword.Text + ";";
string database = "initial catalog=" + txtDataBase.Text + ";";
string cnnString = datasource + yanzheng + database;
ConnectString.Text = cnnString;
Trace.Warn("app", cnnString);
SqlConnection connection = new SqlConnection(cnnString);
SqlDataAdapter adapter = new SqlDataAdapter();
string strSql = "select * from " + "[" + TableName.Text + "]";
SqlCommand command = new SqlCommand(strSql, connection);
adapter.SelectCommand = command;
DataSet dataset = new DataSet();
try
{
connection.Open();
ConnectionResult.Text = "Connection Successful!<br/>";
ConnectionResult.Text += "Server Version:" + connection.ServerVersion + "<br/>";
ConnectionResult.Text += "Database:" + connection.Database + "<br/>";
ConnectionResult.Text += "Status:" + connection.State.ToString() + "<br/>";
ConnectionResult.Text += "TimeOut:" + connection.ConnectionTimeout.ToString() + "<br/>";
adapter.Fill(dataset);
}
catch (Exception e)
{
ConnectionResult.Text = "Connection failed!<br/>";
ConnectionResult.Text += e.ToString();
}
finally
{
connection.Close();
}
DataTable datatable = dataset.Tables[0];
TableRow row;
TableCell cell;
row = new TableRow();
Table1.Rows.Add(row);
foreach (DataColumn datacolumn in datatable.Columns)
{
cell = new TableCell();
cell.Controls.Add(new LiteralControl("<b>" + datacolumn.ColumnName + "</b>"));
row.Cells.Add(cell);
}
foreach (DataRow datarow in datatable.Rows)
{
row = new TableRow();
Table1.Rows.Add(row);
object[] fields = datarow.ItemArray;
foreach (object o in fields)
{
cell = new TableCell();
cell.Controls.Add(new LiteralControl(o.ToString()));
row.Cells.Add(cell);
}
}
}
</script>
</head>
<body>
<h3>Connect to SqlServer</h3>
<form id="form1" runat="server">
<div>
DataSource:<asp:TextBox ID="txtDataSource" runat="server"></asp:TextBox>
Excample:LGIBM5566\LGIBM<br />
User:<asp:TextBox ID="txtUser" runat="server"></asp:TextBox>
Excample:sa;<br />
Password:<asp:TextBox ID="txtPassword" TextMode="Password" runat="server"></asp:TextBox>
Excample:753159;<br />
DataBase:<asp:TextBox ID="txtDataBase" runat="server"></asp:TextBox>
Excample:northwind;<br />
ConnectionString:<br />
<asp:Label ID="ConnectString" runat="server" ></asp:Label>
Result:<br />
<asp:Label ID="ConnectionResult" runat="server" ></asp:Label><br />
Table:<asp:TextBox ID="TableName" runat="server"></asp:TextBox><br />
<asp:Table ID="Table1" GridLines="Both" runat="server">
</asp:Table><br />
<asp:Button ID="Button1" runat="server" OnClick=" OnConnect" Text="連接" />
<hr />
</div>
</form>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -