ASP.NET连接sql2008数据库
1:
using System;
2:
using System.Collections.Generic;
3:
using System.Linq;
4:
using System.Web;
5:
using System.Web.UI;
6:
using System.Web.UI.WebControls;
7:
8:
using System.Data.SqlClient;
9:
using System.Data;
10:
using System.Configuration;
11:
12:
public
partial
class _Default : System.Web.UI.Page
13: {
14:
protected
void Page_Load(
object sender, EventArgs e)
15: {
16:
// 连接sql数据库
17: String sqlconn =
"Data Source=SEEBRO-PC\\SQLEXPRESS;Initial Catalog=SuperMarket;Integrated Security=True";
18: SqlConnection myConnection =
new SqlConnection(sqlconn);
19: myConnection.Open();
20:
21:
//定义SqlCommand类
22: SqlCommand myCommand =
new SqlCommand();
23: myCommand.Connection = myConnection;
24: myCommand.CommandType = CommandType.StoredProcedure;
25: myCommand.CommandText =
"bytype";
26:
//存储过程传参
27: SqlParameter parInput = myCommand.Parameters.Add(
"@type", SqlDbType.SmallMoney);
28: parInput.Direction = ParameterDirection.Input;
29: parInput.Value = 2;
30:
31: SqlDataReader myReader = myCommand.ExecuteReader();
32:
33: Response.Write(
"<table border=1 cellspaceing=0 cellpadding=2>");
34: Response.Write(
"<tr bgcolor=#DAB4B>");
35:
for (
int i = 0; i < myReader.FieldCount; i++)
36: Response.Write(
"<td>" + myReader.GetName(i) +
"</td>");
37: Response.Write(
"</tr>");
38:
39:
while (myReader.Read())
40: {
41: Response.Write(
"<tr>");
42:
for (
int i = 0; i < myReader.FieldCount; i++)
43: Response.Write(
"<td>" + myReader[i].ToString() +
"</td>");
44:
Response.Write(
"</tr>");
45: }
46: Response.Write(
"</table>");
47:
48: myReader.Close();
49: myConnection.Close();
50: }
51: }