使用SQL数据库C#时出现问题

编程入门 行业动态 更新时间:2024-10-25 20:17:35
本文介绍了使用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#时出现问题

本文发布于:2023-11-17 17:15:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1610422.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!