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

时间:2012-02-20 14:51:10

标签: c# .net sql-server sql-server-express sql-server-2012

我试图将文本框值插入名为site_list的数据库表。

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

这是我尝试的代码,当它执行时没有错误,但数据没有显示在表格中

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

任何帮助都将不胜感激。

此致

修改

此代码开始工作

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

5 个答案:

答案 0 :(得分:3)

正如人们所建议的那样,尝试在服务器上创建数据库(使用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();
}

答案 1 :(得分: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();
}

答案 2 :(得分:1)

试试这个:

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();
   }
}
祝你好运

答案 3 :(得分:0)

尝试将文本框值存储在变量中。如:

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

(重要!@stringname中的@不是必需的,但会保护您免受黑客攻击!)

此代码为我创造了奇迹。

答案 4 :(得分:0)

道歉。我之前给出的答案将不起作用,因为insert命令中使用的变量名称(在您的情况下为@site_name)必须与sqlcommand中使用的变量匹配。如:

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

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