能否给个简单的asp.net连接mssql 的例子,不要用控件
我烦控件,像asp一样,比如select后记录放到mystr里,我随时应用.
------解决方案--------------------string mystr = " "; //使用StringBuilder会更好
string sqlConnectionString = "Data Source=; Initial Catalog=; Integrated Security=SSPI "; //windows安全认证
SqlConnection conn = new SqlConnection(sqlConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand( "Select * from [table] ", conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
mystr += (string)dr[filed];
}
dr.Close();
conn.Close();
Response.Write(myStr);
------解决方案--------------------寫成一個class,傳參數進來,就可以隨意調用了!~~~
------解决方案--------------------先顶.
------解决方案--------------------//建立连接
SqlConnection con=new SqlConnection( "server=.;database=vote;uid=sa;pwd=; ");
con.open();
string sqr=( "select * from vote ");
SqlCommand cmd=new SqlCommand(sqr,con);
SqlDataReader sdr=cmd.ExecuteReader;
while(sdr.Reader())
{
Response.Write( "+sdr.GetString(0)+ "- "+sdr.GetInt32(1).ToString()+ ");//接受数据库中列的值
}
sdr.close();
con.close();
------解决方案--------------------转自:http://www.cnblogs.com/yelsea/archive/2007/06/21/792314.html
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
// 创建数据库文件
File.Delete( "test1.db3 ");
SQLiteConnection.CreateFile( "test1.db3 ");
DbProviderFactory factory = SQLiteFactory.Instance;
using (DbConnection conn = factory.CreateConnection())
{
// 连接数据库
conn.ConnectionString = "Data Source=test1.db3 ";
conn.Open();
// 创建数据表
string sql = "create table [test1] ([id] INTEGER PRIMARY KEY, [s] TEXT COLLATE NOCASE) ";
DbCommand cmd = conn.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
// 添加参数
cmd.Parameters.Add(cmd.CreateParameter());
// 开始计时
Stopwatch watch = new Stopwatch();
watch.Start();
// 连续插入1000条记录
for (int i = 0; i < 1000; i++)
{
cmd.CommandText = "insert into [test1] ([s]) values (?) ";
cmd.Parameters[0].Value = i.ToString();
cmd.ExecuteNonQuery();
}
// 停止计时
watch.Stop();
Console.WriteLine(watch.Elapsed);
}
哎~~~~ 一个常识性的错误,我加几行代码 (新增代码标记 "// <------------------- ")。
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
// 创建数据库文件
File.Delete( "test1.db3 ");
SQLiteConnection.CreateFile( "test1.db3 ");
DbProviderFactory factory = SQLiteFactory.Instance;
using (DbConnection conn = factory.CreateConnection())
{
// 连接数据库
conn.ConnectionString = "Data Source=test1.db3 ";
conn.Open();
// 创建数据表
string sql = "create table [test1] ([id] INTEGER PRIMARY KEY, [s] TEXT COLLATE NOCASE) ";
DbCommand cmd = conn.CreateCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
// 添加参数
cmd.Parameters.Add(cmd.CreateParameter());
// 开始计时
Stopwatch watch = new Stopwatch();
watch.Start();