string strConnection = "user id=sa;password=qwertyuiop;";
        strConnection += "Initial Catalog=WebSiteTest;Data Source=OPEN-EQIANG\\SQLEXPRESS;";
        strConnection +="connect timeout=30;";        string strSql = "insert into  tuser (id,username) values(4,'ddd')";        SqlConnection conn = new SqlConnection(strConnection );
手工连接
也可以在WEB.CONFIG里面直接连接

解决方案 »

  1.   

    在VS 2005里可以用“数据源向导”呀,这个比VS 2003的手写要强。
      

  2.   

    using System;
    using System.Data;
    using System.Data.SqlClient;   SqlConnection con = new SqlConnection("server=localhost;uid=登录名;password=密码;database=master;");
            try
            {
                con.Open();
                Response.Write("连接成功");
                con.Close();
            }
            catch(Exception ex)
            {
                Response.Write("数据库连接失败:错误信息:<br/>"+ex.ToString());
            }
      

  3.   

    在web.config中写入以下代码:<?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <connectionStrings>
        <add name="ConnStr1" connectionString="LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=aspnetdb"
          providerName="System.Data.SqlClient" />
      </connectionStrings>
    </configuration>
    然后要使用的时候:// Create a connectionn string element and add it to
    // the connection strings section.
    static ConnectionStrings()
    {
        // Get the application configuration file.
        System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.None);    // Get the current connection strings count.
        int connStrCnt = 
            ConfigurationManager.ConnectionStrings.Count;
     
        // Create the connection string name. 
        string csName = 
            "ConnStr" + connStrCnt.ToString();    // Create a connection string element and
        // save it to the configuration file.
       
        // Create a connection string element.
        ConnectionStringSettings csSettings =
                new ConnectionStringSettings(csName,
                "LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;" +
                "Initial Catalog=aspnetdb", "System.Data.SqlClient");    // Get the connection strings section.
        ConnectionStringsSection csSection =
            config.ConnectionStrings;    // Add the new element.
        csSection.ConnectionStrings.Add(csSettings);
            
        // Save the configuration file.
        config.Save(ConfigurationSaveMode.Modified);
        
    }
      

  4.   

    web.config文件添加:<connectionStrings>
          <!--系統數據庫-->
    <add name="conn" connectionString="Data Source=10.158.88.110;Initial Catalog=mydatabase;User ID=sa;Password=sa" providerName="System.Data.SqlClient"/>
    </connectionStrings>.cs文件中获得连接字符串
      string strconn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
    ----------以返回dataset为例:
     using (SqlConnection connection = new SqlConnection(strconn))
                {
                    DataSet ds = new DataSet();
                    try
                    {
                        connection.Open();
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = connection;
                        cmd.CommandText = SQLString;
                        SqlDataAdapter command = new SqlDataAdapter();
                        command.SelectCommand = cmd;
                        command.Fill(ds, "ds");
                       
                    }
                    catch (System.Data.SqlClient.SqlException ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    finally
                    {
                        connection.Close();
                    }                return ds;
                }-------其实学会了sqldatasource控件的使用就会连接数据库了,建议去下载天轰穿的视频教程看啊  http://www.cnblogs.com/thcjp