Windows表单c#文本框值成功保存在数据库中但未显示在数据库

时间:2018-01-31 05:45:24

标签: windows winforms

它成功执行,但名称未插入DB 这是代码:

        try
        {
            SqlConnection con = new SqlConnection(@"Data
            Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated
            Security=True;");

            con.Open();
            string insertQuery = "insert into userData (Name) values (@Name)";
            SqlCommand com = new SqlCommand(insertQuery, con);
            com.Parameters.AddWithValue("@Name", textBox1.Text);
            com.ExecuteNonQuery();
            MessageBox.Show("thanks");
            con.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("error" + ex);
        }

1 个答案:

答案 0 :(得分:0)

conn.ConnectionString = "Driver={SQL Server};" + "Server=ServerName;" +"DataBase=DataBaseName;" + "Uid=UserName;" + "Pwd=****;"

或者这个

conn.ConnectionString = "Data Source=ServerName;" + "Initial Catalog=DataBaseName;" + "User id=UserName;" + "Password=***;

conn.ConnectionString = "Data Source=ServerName;" + "Initial Catalog=DataBaseName;" + "Integrated Security=true;"

SqlConnection theSqlServer = new SqlConnection(" Server = SQLDevelopment; Database = SQL2008; User ID = sql2008; Password = ****; Trusted_Connection = False;"); 在这里你必须通过以下的事情: - 服务器名称: - 您的数据库服务器位于何处

数据库: - 在该服务器中您要使用哪个数据库

UserId: - 对于数据库中的每个事务(如插入,更新,选择或删除),我们需要使用数据库进行访问,我们需要提供用户Sql server的UserId和密码

Trusted_Connection = True(IS使用Windows凭据,它相当于:Integrated Security = SSPI或Integrated Security = true)