如何插入记录到SQL Server EX preSS数据库中的表? [英] How to insert record into a sql server express database table?

查看:154
本文介绍了如何插入记录到SQL Server EX preSS数据库中的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图插入一个文本框的值称为数据库表 site_list

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

这是在code我想,当它执行没有错误,但该数据未显示在表中了

 的SqlConnection康恩=新的SqlConnection();
conn.ConnectionString =
             数据源= \\ SQLEx preSS; +
             用户实例=真; +
             集成安全性= TRUE; +
             AttachDbFilename = | DataDirectory目录| scraper_db.mdf;;

SqlCommand的addSite =新的SqlCommand(INSERT INTO site_list(SITE_NAME)
     +VALUES(@site_name),康涅狄格州);

addSite.Parameters.Add(@ SITE_NAME,SqlDbType.NVarChar)。价值= textBox1.Text;

conn.Open();
addSite.ExecuteNonQuery();
conn.Close();
 

任何帮助将是AP preciated。

问候

编辑:

这code开始工作。

 字符串CONNSTRING =数据源= \\ SQLEx preSS。+
                        集成安全性= TRUE;+
                        用户实例=真;+
                        AttachDBFilename = | DataDirectory目录| scraper_db.mdf;+
                        初始目录= scraper_db;

    使用(SqlConnection的连接=新的SqlConnection(CONNSTRING))
    {
        connection.Open();
        SqlCommand的addSite =新的SqlCommand(INSERT INTO site_list(SITE_NAME)+
                             VALUES(@site_name),连接);
        addSite.Parameters.AddWithValue(@ SITE_NAME,textBox1.Text);
        addSite.ExecuteNonQuery();
        的Connection.close();
    }
 

解决方案

随着人们建议,尝试在服务器上创建数据库(它会更容易处理使用的 SQL管理工作室)。 一旦这样做了,请尝试以下(只是测试,它的工作原理):

 使用(SqlConnection的康恩=新的SqlConnection(@持续安全信息= FALSE;集成安全=真;初始目录= myTestDb;服务器=(本地)))
{
    SqlCommand的addSite =新的SqlCommand(@INSERT INTO site_list(SITE_NAME)VALUES(@site_name),康涅狄格州);
    addSite.Parameters.AddWithValue(@ SITE_NAME,mywebsitename);
    addSite.Connection.Open();
    addSite.ExecuteNonQuery();
    addSite.Connection.Close();
}
 

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

解决方案

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

这篇关于如何插入记录到SQL Server EX preSS数据库中的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆