2011-03-21 75 views
0

你好嗎,先生,我的代碼是正確的,意味着調試代碼後沒有錯誤。我的目標是,如果用戶點擊按鈕(這是默認的。 aspx),那麼數據庫表就會在數據庫(放在sql express中的數據庫)中創建,我爲此編寫代碼,我們調試代碼並且代碼中沒有錯誤。當我單擊按鈕時(在運行時) 。當我檢查數據庫(這是在SQL Express的),沒有表在database.please先生創建解決我寫在後面的按鈕的C#代碼problem.The是:代碼沒有問題,但輸出沒什麼問題

using System; 
using System.Collections; 
using System.Configuration; 
using System.Data; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 
//using PractiseWeb.DataSet1TableAdapters; 
using System.Collections.Generic; 
using System.Data.OleDb; 
using System.Diagnostics; 
using System.ComponentModel; 
using System.Text; 
using System.Data.SqlClient; 
using System.Data.Odbc; 
using ADOX; 
using ADODB; 

public partial class _Default : System.Web.UI.Page 
{ 
    SqlConnection conn; 
    SqlCommand cmd; 
    string connectionString = ConfigurationManager.ConnectionStrings["gameConnectionString"].ConnectionString; 

    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
    try 
    { 
     conn = new SqlConnection(connectionString); 
     if (!(conn.State == ConnectionState.Open)) 
     { 
     conn.Open(); 
     } 
     string sql = "CREATE TABLE mySchoolRecord(StudentId INTEGER CONSTRAINT PkeyMyId PRIMARY KEY, Name CHAR(50)," + "Address CHAR(255)," + "Contact INTEGER));"; 
     cmd = new SqlCommand(sql, conn); 
     cmd.ExecuteNonQuery(); 

     sql = "INSERT INTO mySchoolRecord (StudentId, Name,Address,Contact) VALUES (1, 'Mr. Manish', " + " 'Sector-12,Noida', 2447658 );"; 
     cmd = new SqlCommand(sql, conn); 
     cmd.ExecuteNonQuery(); 

     sql = "INSERT INTO mySchoolRecord (StudentId, Name,Address,Contact) VALUES (2, 'Mr. Ravi', " + " 'New Delhi', 2584076521 );"; 
     cmd = new SqlCommand(sql, conn); 
     cmd.ExecuteNonQuery(); 

     sql = "INSERT INTO mySchoolRecord (StudentId, Name,Address,Contact) VALUES (3, 'Mr. Peter', " + " 'United States', 25684124 );"; 
     cmd = new SqlCommand(sql, conn); 
     cmd.ExecuteNonQuery(); 


     if (conn.State == ConnectionState.Open) 
     { 
     conn.Close(); 
     } 

    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex); 
    } 

    } 
} 

THANKS SIR

+0

請編輯您的問題。爲你的代碼添加「代碼」格式 – zabulus 2011-03-21 05:57:58

+0

你可以複製你的sql並直接粘貼到sqlserver上,看看它是否創建?在調試時對異常進行中斷,它會告訴你代碼中是否有任何異常。 – kobe 2011-03-21 05:59:38

+0

說「代碼中沒有問題」是......奇怪。 99%的時間,問題在於我們自己的代碼... – 2011-03-21 06:09:15

回答

1

刪除try-catch並查看發生了什麼。寫入控制檯在ASP.NET應用程序中幫助不大。 :)

using System; 
using System.Data; 
using System.Data.SqlClient; 

public partial class _Default : System.Web.UI.Page 
{ 
    SqlConnection conn; 
    SqlCommand cmd; 
    string connectionString = ConfigurationManager.ConnectionStrings["gameConnectionString"].ConnectionString; 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
    using (conn = new SqlConnection(connectionString)) 
    { 
     if (!(conn.State == ConnectionState.Open)) 
     { 
     conn.Open(); 
     } 
     string sql = "CREATE TABLE mySchoolRecord(StudentId INTEGER CONSTRAINT PkeyMyId PRIMARY KEY, Name CHAR(50)," + "Address CHAR(255)," + "Contact INTEGER));"; 
     cmd = new SqlCommand(sql, conn); 
     cmd.ExecuteNonQuery(); 

     sql = "INSERT INTO mySchoolRecord (StudentId, Name,Address,Contact) VALUES (1, 'Mr. Manish', " + " 'Sector-12,Noida', 2447658 );"; 
     cmd = new SqlCommand(sql, conn); 
     cmd.ExecuteNonQuery(); 

     sql = "INSERT INTO mySchoolRecord (StudentId, Name,Address,Contact) VALUES (2, 'Mr. Ravi', " + " 'New Delhi', 2584076521 );"; 
     cmd = new SqlCommand(sql, conn); 
     cmd.ExecuteNonQuery(); 

     sql = "INSERT INTO mySchoolRecord (StudentId, Name,Address,Contact) VALUES (3, 'Mr. Peter', " + " 'United States', 25684124 );"; 
     cmd = new SqlCommand(sql, conn); 
     cmd.ExecuteNonQuery(); 

     if (conn.State == ConnectionState.Open) 
     { 
     conn.Close(); 
     } 
    } 
    } 
} 
0

@sayyer,

按照以下步驟 1)你可以複製你的SQL和粘貼SQLSERVER直接,看看它是否建立或不?不休息的異常而debuggin,它會告訴你,如果有代碼

2任何異常)檢查您的數據庫連接

3)檢查所有格式化的INSERT語句。要解決這個問題

最好的辦法是做CTR + ALT + E和檢查異常打破,這將解決您的問題

0

嘗試從SQL查詢刪除分號。