代码没有问题,但输出什么都没有

时间:2011-03-21 05:56:01

标签: c# asp.net

你好,先生我的代码是正确的意味着在调试代码后没有错误。我的目标是,如果用户点击按钮(例如,置于default.aspx中),则创建数据库表在数据库中(数据库放在sql express中),我为此编写代码我们调试代码并且代码中没有错误。当我点击按钮(在运行时)。当我检查数据库(在sql express)在那个数据库中没有创建表。请先解决我的问题。按钮后面用c#编写的代码是:

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);
    }

  }
}

感谢先生

3 个答案:

答案 0 :(得分: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();
      }
    }
  }
}

答案 1 :(得分:0)

@sayyer,

按照以下步骤操作 1)你可以直接复制你的sql并粘贴在sqlserver上,看它是否创建?在调试时执行异常中断,它会告诉您代码中是否有任何异常

2)检查数据库连接

3)检查插入语句的所有格式。

解决问题的最佳方法是执行ctr + alt + E并检查异常中断,这将解决您的问题

答案 2 :(得分:0)

尝试从SQL查询中删除分号。