一段后台C#查询SQL Server数据库代码

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Text;
using System.Collections;
//using System.Text;

namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strConnection="uid=sa; password=123; ";

strConnection+="initial catalog=aining;Server=192.168.100.3;";

strConnection+="Connect Timeout=30";

SqlConnection objConnection=new SqlConnection(strConnection);

SqlCommand SCom1 = new SqlCommand();//命令对象

objConnection.Open();

DataSet ds = new DataSet();

SqlDataAdapter da = new SqlDataAdapter("select * from M_KAISYA_TAX", objConnection);

int c = da.Fill(ds);

DataRow[] dr = ds.Tables[0].Select();

SCom1.CommandText = "select * from M_SITYOSON";

SCom1.Connection = objConnection;

SCom1.CommandType = System.Data.CommandType.Text;

//int num = SCom1.ExecuteNonQuery();

//SqlDataAdapter adapter = new SqlDataAdapter(SCom1);

SqlDataReader sdr= SCom1.ExecuteReader();

test11.Value = "Hellol";
IList data = new ArrayList();
StringBuilder sbSql = new StringBuilder();

while (sdr.Read())
{
test11.Value = sdr.GetString(1);
for (int i = 0; i < sdr.FieldCount; i++)
{
if (!(sdr.IsDBNull(i)))
{
if (!(sdr[i] is String))
{
continue;
}
sbSql.Append(sdr.GetString(i).ToString());
}

}

data.Add(sdr[0].ToString());
}


sdr.Close();


//test11.Value = dr[0][0].ToString();
//test11.Value =SCom1.

objConnection.Close();







//SqlConnection SCON = null;//连接对象
//SqlCommand SCom = new SqlCommand();//命令对象
//SCON = new SqlConnection(strConnection);
//SCON.Open();
//SCom.CommandText = "insert into cum(nm,age,sex,addr) values(‘”+name1+”’, ‘”+age1+”’, ‘”+sex1”’,’”+addr1+”’); insert into cum(nm,age,sex,addr) values(‘”+name1+”’, ‘”+age1+”’, ‘”+sex1”’,’”+addr1+”’)";
//SCom.Connection = SCON;
//SCom.CommandType = CommandType.Text;
//int num = SCom.ExecuteNonQuery();
//SCON.Close();



//string str = "server=.;uid=sa;pwd=111111;database=text_db";//连接字符串
//SqlConnection SCON;
//SqlCommand SCom1 = new SqlCommand();//要执行sql语句的第一个命令对象
//SqlCommand SCom2 = new SqlCommand();//要执行sql语句的第二个命令对象
//SCON = new SqlConnection(str);
//SCON.Open();
////第一条要执行的sql语句
//SCom1.CommandText = "insert into cum(nm,age,sex,addr) values(@name,@age,@sex,@addr)";
//SCom1.Parameters.Add("@name", SqlDbType.Text);
//SCom1.Parameters.Add("@age", SqlDbType.Int);
//SCom1.Parameters.Add("@sex", SqlDbType.Bit);
//SCom1.Parameters.Add("@addr", SqlDbType.Text);
//SCom1.Parameters["@name"].Value = name1;
//SCom1.Parameters["@age"].Value = age1;
//SCom1.Parameters["@sex"].Value = sex1;
//SCom1.Parameters["@addr"].Value = addr1;
//SCom1.Connection = SCON;
//SCom1.CommandType = CommandType.Text;
//int num = SCom1.ExecuteNonQuery();
////第二条要招行的sql语句
//SCom2.CommandText = "insert into cum(nm,age,sex,addr) values(@name,@age,@sex,@addr)";
//SCom2.Parameters.Add("@name", SqlDbType.Text);
//SCom2.Parameters.Add("@age", SqlDbType.Int);
//SCom2.Parameters.Add("@sex", SqlDbType.Bit);
//SCom2.Parameters.Add("@addr", SqlDbType.Text);
//SCom2.Parameters["@name"].Value = name2;
//SCom2.Parameters["@age"].Value = age2;
//SCom2.Parameters["@sex"].Value = sex2;
//SCom2.Parameters["@addr"].Value = addr2;
//SCom2.Connection = SCON;
//SCom2.CommandType = CommandType.Text;
//int num = SCom2.ExecuteNonQuery();
//SCON.Close();





}
}
}