使用SQL数据库C#时出现问题 [英] Problem Using SQL database C#

查看:55
本文介绍了使用SQL数据库C#时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在c#中使用数据库,因为我已经创建了一个检查或添加用户的应用程序
问题是
1.无论何时运行解决方案,值都会添加到数据库中并显示出来,但是一旦我重新编译解决方案,所有先前的数据都将消失
2.无论我存储在数据库中的数据是什么,它的末尾都存储有空格,例如,在文本框中输入的用户名是Afnan,它存储并以空格显示为Afnan ........代表空间.
我添加了MDF文件并在其中创建了表.


I am learning how to use database in c# for that i have created an application that checks or adds user
Problem is that
1.when ever solution is running the values are added to database and are shown but as soon as i recompile the solution then all the previous data is gone
2.what ever data i store in database it gets stored with spaces at its end for eg user name entered in text box is Afnan it gets stored and displayed with spaces as Afnan........ where . represent space.
I added MDF file and created table in that.


using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.Data.SqlClient;<br />
using System.Data.Sql;<br />
namespace Neo_Soft_Project_Manegment<br />
{<br />
    public partial class Form1 : Form<br />
    {<br />
        public Form1()<br />
        {<br />
            InitializeComponent();<br />
        }<br />
<br />
     <br />
        private void Form1_Load(object sender, EventArgs e)<br />
        {<br />
<br />
           // MessageBox.Show(usersTableAdapter1.);<br />
<br />
        }<br />
<br />
        private void button_login_Click(object sender, EventArgs e)<br />
        {<br />
     <br />
            string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";<br />
            SqlConnection cn = new SqlConnection(connectionString);<br />
            try<br />
            {<br />
                cn.Open();<br />
              <br />
            }<br />
            catch<br />
            {<br />
                MessageBox.Show("Unable to connect");<br />
            }<br />
<br />
            string Uname = text_user.Text;<br />
            string Pass = text_pass.Text;<br />
            string sqlquery = ("SELECT*FROM Users WHERE user_name = ''" + text_user.Text + "''");<br />
            sqlquery = " INSERT INTO [Users](user_name,user_pass) VALUES (''"+text_user.Text+"'',''"+text_pass.Text+"'')";<br />
            SqlCommand command = new SqlCommand(sqlquery, cn);<br />
            command.ExecuteNonQuery();<br />
            <br />
            if (command != null)<br />
                command.Dispose();<br />
            if (cn != null)<br />
                cn.Close();            <br />
        }<br />
<br />
        private void button1_Click(object sender, EventArgs e)<br />
        {<br />
            string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";<br />
            SqlConnection cn = new SqlConnection(connectionString);<br />
            try<br />
            {<br />
                cn.Open();<br />
            }<br />
            catch<br />
            {<br />
                MessageBox.Show("Unable to connect");<br />
            }<br />
            SqlCommand cmd = new SqlCommand("SELECT * FROM Users", cn);<br />
            cmd.Connection = cn;<br />
            SqlDataReader reader = null;<br />
            reader = cmd.ExecuteReader();<br />
            while (reader.Read())<br />
            {<br />
                MessageBox.Show(reader["user_name"].ToString() + reader["user_pass"].ToString());<br />
                if (text_user.Text == (reader["user_name"].ToString()) && text_pass.Text == (reader["user_pass"].ToString()))<br />
                {<br />
                    MessageBox.Show("Loged In");<br />
                }<br />
                else MessageBox.Show("Failed to Login");<br />
            }<br />
        }<br />
<br />
   }<br />
}<br />

推荐答案

如果您将SQL Express Server与用户实例一起使用:

1.在构建过程中,将数据库从源复制到构建目标文件夹.
2.执行的应用程序使用复制的数据库(例如来自bin \ debug的数据库)
3.重建/重新运行后,您的数据库将替换为源中存储的原始数据库(可能是完全空的数据库).


值末尾的空格可能是由所选的数据类型(例如char或nchar)引起的.尝试使用varchar或nvarchar插入.
If you are using SQL Express Server with user instance:

1. during build is copied the database from sources to the build target folder.
2. executed application uses the copied database (such as one from bin\debug)
3. after rebuild/rerun is your database replaced by the original one stored in sources (probably entirely empty one).


Spaces at the end of value may be caused by choosen datatype (such as char or nchar). Try varchar or nvarchar insted if it''s the case.


确保未将MDF文件设置为复制到输出目录.

在解决方案资源管理器中,检查一下构建操作是什么,并确保它不会覆盖数据库.

干杯.
Make sure your MDF file is not set to be copied to your output directory.

In Solution Explorer, check to see what the build action is and make sure it''s not overwriting your database.

Cheers.


詹姆斯先生,如果我将MDF文件设置更改为不复制,则数据库连接失败
Mr James if I change MDF file settings to do not copy then database connection fails


这篇关于使用SQL数据库C#时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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