![](/img/trans.png)
[英]Unable to insert data into MySQL table via ASP.NET Web Application
[英]Insert SQL data into table in ASP.Net Web Application via textbox
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
namespace todoassignment1
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Visible = false;
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string valueUser = txtUsername.Text;
SqlConnection db = new SqlConnection(SqlDataSource1.ConnectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
//User is the name of the table, UserName is the column
cmd.CommandText = "INSERT User (UserName) VALUES ('" + valueUser + "')";
cmd.Connection = db;
db.Open();
try
{
cmd.Connection.Open();
cmd.ExecuteNonQuery();
Label1.Text = "Success writing into database!";
Label1.Visible = true;
}
catch (Exception ex)
{
Label1.Text = "Error writing into database.";
Console.WriteLine(ex.Message);
Label1.Visible = true;
}
finally
{
db.Close();
}
}
}
}
我在Default.aspx頁上添加了SqlDataSource1,這似乎已成功完成,因為我可以在gridview中看到User表的列名。 該表為空,我要做的就是在文本框中輸入內容並將其發送到UserName列。 我不是很擅長調試,但是我注意到我花了幾個小時研究的一些錯誤。 我嘗試過的事情:
使用web.config中的確切字符串創建新的SQLConnection
SqlConnection db = new SqlConnection("Data Source=NAME-PC\\\\SQLEXPRESS;Initial Catalog=Assignment;Integrated Security=True");
為了簡化代碼,將插入行從串聯更改為僅“ INSERT User(UserName)VALUES('asdf')”,但這仍然行不通。
嘗試其他語法
cmd.CommandText = "INSERT INTO User (UserName) VALUES (@username)"; cmd.Parameters.Add("@username", SqlDbType.NVarChar, 50); cmd.Parameters["@username"].Value = txtUsername.Text.ToString();
來自調試器的線索:
錯誤CS0103:名稱“ ExecuteNonQuery”在當前上下文中不存在
運行后總是看到System.InvalidOperationException
請幫忙。
static void BtnSubmit_Click(object sender, EventArgs e)
{
string valueUser = "test"; //txtUsername.Text;
using (SqlConnection db = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=test;Integrated Security=True"))//SqlDataSource1.ConnectionString);
{
SqlCommand cmd = new SqlCommand("INSERT INTO dbo.[User] (UserName) VALUES (@valueUser)", db);
cmd.Parameters.Add("@valueUser", SqlDbType.VarChar, 50).Value = valueUser;
//cmd.CommandType = CommandType.Text;
//User is the name of the table, UserName is the column
//cmd.CommandText = "INSERT User (UserName) VALUES (@valueUser)";
//cmd.Connection = db;
db.Open();
try
{
//cmd.Connection.Open();
cmd.ExecuteNonQuery();
/*Label1.Text =*/
Console.WriteLine("Success writing into database!");
//Label1.Visible = true;
}
catch (Exception ex)
{
/*Label1.Text =*/
Console.WriteLine("Error writing into database.");
Console.WriteLine(ex.Message);
//Label1.Visible = true;
}
//finally
//{
//db.Close();
//}
}
}
有幾個問題。
INSERT INTO TABLE (COLUMNS) VALUES (VALUES)
您有“ INSERT User(UserName)” ... USER
是SQL中的保留字。 如果您堅持使用它作為表名,則必須將其用方括號括起來。 如果您修改了我附帶的方法,則應該可以復制粘貼它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.