简体   繁体   English

如何将记录插入sql server express数据库表?

[英]How to insert record into a sql server express database table?

I'm trying to insert a textbox value to a database table called site_list . 我正在尝试将文本框值插入名为site_list的数据库表。

The site_list table contains two columns id and site_name , id set to auto increment site_list表包含两列idsite_nameid设置为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();
    }

as people suggests, try creating the database on the server (it will be even easier to handle using Sql Management Studio ). 正如人们所建议的那样,尝试在服务器上创建数据库(使用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();
}
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();
}

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 祝好运

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!) (重要!@stringname中的@不是必需的,但保护您免受黑客攻击!)

This code has worked wonders for me. 这段代码为我创造了奇迹。

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. 我之前给出的答案将不起作用,因为insert命令中使用的变量名称(在您的情况下为@site_name)必须与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. 很抱歉我可能造成了混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM