问题
I'm trying to insert a textbox value to a database table called site_list
.
The site_list
table contains two columns id
and site_name
, id
set to auto increment
This is the code I'm trying and when it execute there is no error, but the data is not showing up in the table
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
"Data Source=.\\SQLExpress;" +
"User Instance=true;" +
"Integrated Security=true;" +
"AttachDbFilename=|DataDirectory|scraper_db.mdf;";
SqlCommand addSite = new SqlCommand("INSERT INTO site_list (site_name) "
+ " VALUES (@site_name)", conn);
addSite.Parameters.Add("@site_name", SqlDbType.NVarChar).Value = textBox1.Text;
conn.Open();
addSite.ExecuteNonQuery();
conn.Close();
Any help would be appreciated.
Regards
Edit:
This code started to work
string connstring = "Data Source=.\\SQLExpress;"+
"Integrated Security=true;"+
"User Instance=true;"+
"AttachDBFilename=|DataDirectory|scraper_db.mdf;"+
"Initial Catalog=scraper_db";
using (SqlConnection connection = new SqlConnection(connstring))
{
connection.Open();
SqlCommand addSite = new SqlCommand("INSERT INTO site_list (site_name)"+
"VALUES (@site_name)", connection);
addSite.Parameters.AddWithValue("@site_name", textBox1.Text);
addSite.ExecuteNonQuery();
connection.Close();
}
回答1:
as people suggests, try creating the database on the server (it will be even easier to handle using Sql Management Studio). Once that's done, try the following (just tested and it works):
using (SqlConnection conn = new SqlConnection(@"Persist Security Info=False;Integrated Security=true;Initial Catalog=myTestDb;server=(local)"))
{
SqlCommand addSite = new SqlCommand(@"INSERT INTO site_list (site_name) VALUES (@site_name)", conn);
addSite.Parameters.AddWithValue("@site_name", "mywebsitename");
addSite.Connection.Open();
addSite.ExecuteNonQuery();
addSite.Connection.Close();
}
回答2:
try
{
using (SqlConnection conn = new SqlConnection(@"Persist Security Info=False;Integrated Security=true;Initial Catalog=myTestDb;server=(local)\SQLEXPRESS;database=Inventory;Data Source=localhost\SQLEXPRESS;"))
{
SqlCommand addSite = new SqlCommand(@"INSERT INTO Creation (Name,Product,Quantity,Category) VALUES (@Name,@Product,@Quantity,@Category)", conn);
addSite.Parameters.AddWithValue("@Name", textBox1.Text);
addSite.Parameters.AddWithValue("@Product", textBox2.Text);
addSite.Parameters.AddWithValue("@Quantity", textBox3.Text.ToString());
addSite.Parameters.AddWithValue("@Category", textBox4.Text);
thisConnection.Open();
addSite.ExecuteNonQuery();
}
}
catch
{
thisConnection.Close();
}
回答3:
try this out :
string sql = String.Format("INSERT INTO site_list (site_name) VALUES('{0}')", myTextBox.Text);
using(SqlConnection connection = new SqlConnection(myConnectionString))
{
connection.open();
using(SqlCommand cmd = new SqlCommand(sql, connection))
{
cmd.ExecuteNonQuery();
}
}
Good luck
回答4:
Try storing your textbox value in a variable. As in:
@stringname = textbox1.text
addSite.Parameters.Add("@site_name", SqlDbType.NVarChar).Value = @stringname;
(IMPORTANT! the @ in @stringname is not necessary, but protects you against hackers!)
This code has worked wonders for me.
回答5:
My apologies. The answer I gave previously will not work because the variable name used in the insert command (in your case @site_name) must match the variables used in your sqlcommand. As in:
@site_name = textbox1.text
addSite.Parameters.Add("@site_name", SqlDbType.NVarChar).Value = textBox1.Text;
Sorry for the confusion I might have caused.
来源:https://stackoverflow.com/questions/9363061/how-to-insert-record-into-a-sql-server-express-database-table