本文介绍了如果用户单击按钮,然后使用c Sharp通过asp.net在数据库(sql express)中创建数据库表,该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,您好,先生,我的问题是,如果用户单击按钮(例如,位于default.aspx中的按钮),那么将在数据库(即SQL Express)中创建数据库表.做到这一点.请先生帮助我解决这个问题.我尝试了一下,但是在调试过程中发生错误,错误是:

1.``system.data.odbc.odbc command.odbc command(string,system.data.odbc.odbc connection)''的最佳重载方法匹配具有无效参数.
2.参数``2'':无法从``system.data.sqlclient.sqlconnection''转换为``system.data.odbc.odbc连接''.

按钮后面用c#编写的代码(例如,放置在default.aspx中)是

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;
      OdbcCommand 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 OdbcCommand(sql,conn);// in this line above two errors occurred
    cmd.ExecuteNonQuery(); 
    sql = "INSERT INTO mySchoolRecord (StudentId, Name,Address,Contact) VALUES (1, ''Mr. Manish'', " + " ''Sector-12,Noida'', 2447658  );";
    cmd = new OdbcCommand(sql,conn);// in this line above two errors occurred
    cmd.ExecuteNonQuery(); 
    sql = "INSERT INTO mySchoolRecord (StudentId, Name,Address,Contact) VALUES (2, ''Mr. Ravi'', " + " ''New Delhi'', 2584076521   );"; 
    cmd = new OdbcCommand(sql,conn);// in this line above two errors occurred
    cmd.ExecuteNonQuery();
    sql = "INSERT INTO mySchoolRecord (StudentId, Name,Address,Contact) VALUES (3, ''Mr. Peter'', " + " ''United States'', 25684124  );"; 
    cmd = new OdbcCommand(sql,conn);// in this line above two errors occurred
    cmd.ExecuteNonQuery(); 

    if (conn.State == ConnectionState.Open) 
    { 
        conn.Close(); 
    } 
 
} 
catch (OdbcException ex) 
{ 
   Console.WriteLine(ex); 
} 
    }
}

plz先生,请通过电子邮件将我发送至该电子邮件:[REMOVED] @ yahoo.com,如果您知道或链接到任何解决方案. >解决方案



hello sir, how are you,sir my question is that if the user click on button( which is place in default.aspx,for example) then the database table is created in database(that is SQL express),how can do that.Please sir help me in that problem.I try that thing but error is occurred during debugging,errors are:

1.the best overloaded method match for ''system.data.odbc.odbc command.odbc command(string,system.data.odbc.odbc connection)''has some invalid arguments.
2.Argument''2'':cannot convert from ''system.data.sqlclient.sqlconnection'' to ''system.data.odbc.odbc connection''.

The code written in c# behind the button( which is placed in default.aspx,for example) is that

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;
      OdbcCommand 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 OdbcCommand(sql,conn);// in this line above two errors occurred
    cmd.ExecuteNonQuery(); 
    sql = "INSERT INTO mySchoolRecord (StudentId, Name,Address,Contact) VALUES (1, ''Mr. Manish'', " + " ''Sector-12,Noida'', 2447658  );";
    cmd = new OdbcCommand(sql,conn);// in this line above two errors occurred
    cmd.ExecuteNonQuery(); 
    sql = "INSERT INTO mySchoolRecord (StudentId, Name,Address,Contact) VALUES (2, ''Mr. Ravi'', " + " ''New Delhi'', 2584076521   );"; 
    cmd = new OdbcCommand(sql,conn);// in this line above two errors occurred
    cmd.ExecuteNonQuery();
    sql = "INSERT INTO mySchoolRecord (StudentId, Name,Address,Contact) VALUES (3, ''Mr. Peter'', " + " ''United States'', 25684124  );"; 
    cmd = new OdbcCommand(sql,conn);// in this line above two errors occurred
    cmd.ExecuteNonQuery(); 

    if (conn.State == ConnectionState.Open) 
    { 
        conn.Close(); 
    } 
 
} 
catch (OdbcException ex) 
{ 
   Console.WriteLine(ex); 
} 
    }
}

plz sir email me to that email:[REMOVED]@yahoo.com,if any solution you know or link.plz sir solution or link in detial through which i can easily understand.THANKS SIR

解决方案



这篇关于如果用户单击按钮,然后使用c Sharp通过asp.net在数据库(sql express)中创建数据库表,该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 13:29