新手菜鸟潇关于winform数据库连接问题

关于数据库连接问题首先是进行config文件的配置:

1.1.window身份验证以及读取字段:

<appSettings>
    <add key="ConnectString" value="server=.\sqlexpress;database=BBSDB;integrated security=sspi"/>
</appSettings>

其中integrated security是Microsoft安全支持提供器接口(sspi)定义得较全面的公用API,用来获得验证、信息完整性、信息隐私等集成安全服务;

==============

Integrated Security 身份验证方式 
当为false时,将在连接中指定用户ID和密码。 
当为true时,将使用当前的Windows帐户凭据进行身份验证。 
可识别的值为true、false、yes、no以及与true等效的sspi。

==============

C#code

string comm = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;

或者


1.2.SQL Server身份验证:

<connectionStrings>
    <add name ="connection" connectionString="server=127.0.0.1;database=login;uid=sa;pwd=liandeng;" />
  </connectionStrings>

c#中读取字段代码:

string comm = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;

其中记得添加引用system.webfiguration

2.

private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string username = textBox1.Text.Trim();
                string password = textBox2.Text.Trim();
                if (username == "")
                {
                    MessageBox.Show("请输入用户名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (password == "")
                {
                    MessageBox.Show("请输入密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    string comm = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;//读取数据库
                    SqlConnection con = new SqlConnection(comm);//进行连接;
                    con.Open();//打开数据库;
                    SqlCommand com = new SqlCommand();//创建数据库命令,才可以进行增删查改;
                    com.Connection = con;
                    com.CommandType = CommandType.Text;//读取SQL文本;
                    com.CommandText = "select count(*) from login where name='" + username + "' and password='" + password + "'";
                    //查询语句;
                    //SqlDataReader dr = com.ExecuteReader();//进行查询返回所有值;
                    int n = (int)com.ExecuteScalar();//进行查询返回第一行;
                    if (n > 0)
                    {
                        //dr.Close();
                        con.Close();
                        MessageBox.Show("登陆成功!");
                    }
                    else
                    {
                        MessageBox.Show("用户名或密码错误", "警告", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值