[英]if the user click on button then the database table is created in database through asp.net with c#,how can do?
我的問題是,如果用戶單擊按鈕(例如,放置在default.aspx中的按鈕),則在數據庫(SQL Express)中創建數據庫表,該怎么辦?
我已經嘗試過,但是在調試過程中發生了錯誤,錯誤是:
the best overloaded method match for 'system.data.odbc.odbc command.odbc command(string,system.data.odbc.odbc connection)'has some invalid arguments.
Argument'2':cannot convert from 'system.data.sqlclient.sqlconnection' to 'system.data.odbc.odbc connection'.
按鈕后面(例如,按鈕放置在default.aspx中)用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;
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);
}
}
}
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
public object connection()
{
SqlConnection myConnectionString = new SqlConnection("Data Source=(local)\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True;Pooling=False");
double[] a = new double[6];
string myCommand = null;
myCommand = "CREATE database soft_billing";
SqlCommand cmd = new SqlCommand(myCommand, myConnectionString);
try {
for (int k = 0; k <= 4; k++) {
a(k) = 0.0;
}
cmd.Connection.Open();
cmd.ExecuteNonQuery();
t.Enabled = true;
t.Interval = 10;
ProgressBar1.Value = 0;
cmd.Connection.Close();
} catch {
Interaction.MsgBox(" Already installed database", MsgBoxStyle.Critical, " MaS InfoTech- Warning");
}
try {
SqlConnection cn = new SqlConnection("Data Source=(local)\\SQLEXPRESS;Initial Catalog=soft_billing;Integrated Security=True;Pooling=False");
string sql = null;
sql = "CREATE TABLE customer(cus_name varchar(50) NULL,address varchar(50) NULL,mobno numeric(18, 0) NULL,tin varchar(50) NULL,kg varchar(50) NULL)";
cmd = new SqlCommand(sql, cn);
// , connection);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
} catch {
Interaction.MsgBox(" Already installed database", MsgBoxStyle.Critical, " MaS InfoTech- Warning");
}
}
您正在混合使用ADO(SQLConnection)和ODBC(ODBCCommand)。 如果使用的是SQL Server,請改用SQLCommand;否則請使用ODBCCommand 。 您不能將兩者混在一起。
嘗試這個
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 SqlCommand(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 SqlCommand(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 SqlCommand(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 SqlCommand(sql,conn);// in this line above two errors occurred
cmd.ExecuteNonQuery();
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
catch (OdbcException ex)
{
Console.WriteLine(ex);
}
}
}
用SqlCommand替換OdbcCommand將解決您的問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.