C# 如何将记录插入到 sql server express 数据库表中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9363061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:02:21  来源:igfitidea点击:

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

c#.netsql-serversql-server-expresssql-server-2012

提问by Gihan Lasita

I'm trying to insert a textbox value to a database table called site_list.

我正在尝试将文本框值插入到名为site_list.

The site_listtable contains two columns idand site_name, idset to auto increment

site_list表包含两列idsite_nameid设置为自动递增

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();
    }

采纳答案by Giorgio Minardi

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):

正如人们所建议的,尝试在服务器上创建数据库(使用Sql Management Studio处理会更容易)。完成后,请尝试以下操作(刚刚测试并有效):

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();
}

回答by SidAhmed

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

祝你好运

回答by ronak

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();
}

回答by Herold van de Ven

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.

这段代码为我创造了奇迹。

回答by Herold van de Ven

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)必须与您的 sqlcommand 中使用的变量匹配。如:

@site_name = textbox1.text
addSite.Parameters.Add("@site_name", SqlDbType.NVarChar).Value = textBox1.Text;

Sorry for the confusion I might have caused.

很抱歉我可能造成的混乱。