2016-04-22 217 views
-1
public partial class WebForm1 : System.Web.UI.Page 
{ 
    SqlConnection con=new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"); 
    SqlDataAdapter da = new SqlDataAdapter(); 
    SqlCommand cmd = new SqlCommand(); 
    DataTable dt = new DataTable(); 


    protected void Page_Load(object sender, EventArgs e) 
    { 
    cmd.CommandText="select id from regtb where id="+Session["id"]; 
    da.SelectCommand = cmd; 
    da.Fill(dt); 
    if(dt.Rows.Count>0) 
    { 
     Response.Redirect("userlogin.aspx"); 

    } 

    } 

此代碼有什麼問題? 任何人都可以幫我在這 我得到這個錯誤,我不知道如何解決這個問題。填充:SelectCommand.Connection屬性尚未初始化C#

+0

不要使用MDF數據庫文件。使用服務器中數據庫的名稱。你可以讓證書問題直接訪問mdf文件。我會通過使用SQL Server Management Studio(SSMS)來確保您擁有適當的憑據,並嘗試從SSMS進行查詢以確保連接正確。然後使用SSMS登錄窗口中的服務器名稱。 – jdweng

回答

1

你需要打開你的SqlConnection,然後將其分配給SqlCommand:

   con.Open(); 
       cmd.Connection = con; 
相關問題