导航

C#笔记之数据库连接

Posted on 2005-01-20 12:21  Roson's Blog  阅读(284)  评论(0)    收藏  举报

一.web.config方式
<configuration>
  <appSettings>
    <add key="ConnectionString" value="Server=(local);UID=sa;PWD=sa;Database=pubs" />
  </appSettings>
...

code - behinds:
====================
using System.Data.Sqlconn;
using System.Configuration;

string strsql;

SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

1).
SqlDataAdapter da=new SqlDataAdapter(strsql,conn);
DataSet ds=new DataSet();
da.Fill(ds);

//return ds;

2).
SqlCommand cm = new SqlCommand(strsql, conn);
conn.Open();
cm.ExecuteNonQuery();
conn.Close();

//no return

3).
SqlCommand cm = new SqlCommand(strsql, conn);
conn.Open();
object temp = cm.ExecuteScalar();
conn.Close();

//return (int)temp