C#连接SQL Sever 数据库

 1.引用

using System.Data.SqlClient;//引入数据库

 2.连接字符串

string connString148 = @"server=127.0.0.1;uid=sa;pwd=cc3279706705;database=xs";//连接字符串

3.登录按钮里面的代码

if (txtID.Text == "" || txtPassWord.Text == "")
            {

                MessageBox.Show("请输入完整用户名和密码!", "登录程序");
                txtID.Focus();//该控件获得焦点

            }
            else
            {
                try
                {
                    string sql19148 = string.Format(@"select username, password from users
                     where username like '{0}' and password like '{1}' ", txtID.Text.Trim(), txtPassWord.Text.Trim());

                    using (SqlConnection cn148 = new SqlConnection(connString148))
                    {
                        cn148.Open();
                        SqlCommand cm148 = new SqlCommand(sql19148, cn148);
                        int i = Convert.ToInt32(cm148.ExecuteScalar());

                        if (i >= 1)
                        {

                            //new frmMain().Show();
                            this.Close();                            

                        }
                        else
                        {
                            MessageBox.Show("账号或密码错误");
                            txtID.Text = "";
                            txtPassWord.Text = "";
                            txtID.Focus();

                        }
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Unity中连接SQL Server数据库,你需要使用System.Data.SqlClient命名空间提供的一些类。以下是一些基本步骤: 1. 首先,你需要在Unity项目中添加对System.Data.dll程序集的引用。可以在Visual Studio中完成此操作,也可以使用Unity的“Assets”菜单中的“Import Package”选项。 2. 接下来,你需要编写C#代码来连接SQL Server数据库。以下是一个简单的示例: ``` using System.Data.SqlClient; string connectionString = "Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"; SqlConnection connection = new SqlConnection(connectionString); connection.Open(); ``` 在这个示例中,你需要将“myServerAddress”替换为SQL Server的主机名或IP地址,“myDataBase”替换为要连接数据库名称,“myUsername”和“myPassword”替换为SQL Server登录凭据。 3. 一旦连接建立,你可以使用SqlCommand类来执行SQL查询。以下是一个示例: ``` string query = "SELECT * FROM myTable"; SqlCommand command = new SqlCommand(query, connection); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { // 处理查询结果 } ``` 在这个示例中,你需要将“myTable”替换为要查询的表的名称。使用SqlDataReader类可以逐行读取查询结果,并对每行进行处理。 这只是连接SQL Server数据库的基本步骤,你可能需要进行更多的配置和错误处理。但是这应该可以帮助你开始了解如何在Unity中连接SQL Server数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值