如何使用C#在SQL数据库中保存用户详细信息 [英] How do I save my user details in SQL database using C#

查看:183
本文介绍了如何使用C#在SQL数据库中保存用户详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的用户名密码保存在SQL数据库中,但它不起作用。



我尝试了什么:



am trying to get my username n password saved in SQL Database but it doesnt work.

What I have tried:

i have tried using the following codes
<pre> private void btnLogin_Click(object sender, EventArgs e)
        {

            SqlConnection cs = new SqlConnection("Data Source =Tyiselani-HP, Initial Catalog =Customers, Intergrated Security = TRUE");
            SqlDataAdapter dc = new SqlDataAdapter();
            dc.InsertCommand = new SqlCommand("INSERT INTO Login1 VALUES (@Username,@Password)", cs);
            dc.InsertCommand.Parameters.Add("@Username", SqlDbType.VarChar).Value = Txtusername.Text;
            dc.InsertCommand.Parameters.Add("@Password", SqlDbType.VarChar).Value = Txtpassword.Text;
            
            cs.Open();
            dc.InsertCommand.ExecuteNonQuery();
            cs.Close();

推荐答案

这里有很多问题,没有一个与你的问题直接相关......

1)从不硬编码连接字符串:使用配置文件(或至少是一个const字符串,因此只需要在一个地方输入)。

2 )连接,命令等作为稀缺资源 - 您需要在完成后正确处理它们。 使用块是最简单的方法。

3)不要使用DataAdapter进行简单的单行插入:这非常浪费。生成一个独立的SqlCommand对象并使用它。

4)永远不要以明文形式存储密码 - 这是一个主要的安全风险。有关如何在此处执行此操作的信息:密码存储:如何做到这一点。 [ ^ ]



解决这些问题,你的问题可能会同时消失......
Loads of problems here, none of them directly related to your problem...
1) Never hard code connection strings: use a config file (or at the very least a const string so it only needs to be entered in one place).
2) Connections, Commands, and so forth as scarce resources - you need to Dispose them properly when complete. A using block is the simplest way to do that.
3) Don't use a DataAdapter for a simple one line insert: it's very wasteful. Generate a stand-alone SqlCommand object and use that instead.
4) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

Fix those, and your problem will probably vanish at the same time...


这篇关于如何使用C#在SQL数据库中保存用户详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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