.net2005编写c#应用程序时,怎样实现与SQL Server 数据库连接啊?初学者拜托各位了

解决方案 »

  1.   

    using System.Data;
    using System.Data.SqlClient;//联结 conn
            SqlConnection SqlConn = null;public bool Connect(string username, string pass, string dataname, string ip)
            {
                string connstr = "User ID=" + username + ";Initial Catalog=" + dataname + ";Data Source=" + ip + ";Password=" + pass;            try
                {
                    SqlConn = new SqlConnection(connstr);
                    SqlConn.Open();
                    return true;
                }
                catch
                {
                    Disconnect();
                    return false;
                }
            }public void Disconnect()
            {
                if (SqlConn != null)
                {
                    SqlConn.Close();
                    SqlConn = null;
                }
            }
      

  2.   

    我做了一个程序,用到了抛出异常,但就是不知道是什么异常,用了try,catch才好,但是不明白什么意思
     private void butYes_Click(object sender, EventArgs e)
            {
                if (this.txtUser.Text == "")
                {
                    MessageBox.Show("用户名不能为空!");
                    this.txtUser.Focus();
                    return;
                }
                else
                {
                    if (this.txtPwd.Text == "")
                    {
                        MessageBox.Show("密码 不能为空!");
                        this.txtPwd.Focus();
                        return;
                    }
                    else 
                    { 
                        string commandString="select * from operator where Op_user='"+this.txtUser.Text.ToString()+"'and Op_password ='"+this.txtUser.Text.ToString()+"'";
                        SqlDataAdapter sql = new SqlDataAdapter(commandString,Scon);
                        DataSet ds = new DataSet();
                        try
                        {
                            sql.Fill(ds, "operator");                    }
                        catch (Exception e1)
                        {
                           MessageBox.Show(e1.Message);
                        }
                        int h = ds.Tables[0].Rows.Count;
                        if (h == 0)
                        {
                            MessageBox.Show("用户名和密码输入错误!!");
                            this.txtUser.Text = this.txtPwd.Text = "";
                            this.txtUser.Focus();
                            return;
                        }
                        else
                        {
                            user_name = ds.Tables[0].Rows[0][2].ToString();
                            user_level = ds.Tables[0].Rows[0][4].ToString();
                            this.Hide();
                            Main_info mi = new Main_info();
                            mi.Show();
                        }
                    }
                    
                }
                        }
      

  3.   

    e1.Message 显示了什么?这种sql登陆方式很不安全。
    应该搜索用户名,取出口令,然后与输入的口令判断,这样好些。
      

  4.   

    连接数据库得到string user=select op_password from operator where Op_user='"+this.txtUser.Text.ToString()+"'
    然后if 比较user == txtpwd.text.toString(),是这样吗?
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data.SqlClient;
    using System.Data;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                // "uid=sa":连接数据库的用户名为sa.
                // "password=":连接数据库的验证密码为空.他的别名为"pwd",所以我们可以写为"pwd=".
                // "initial catalog=Northwind":使用的数据源为"Northwind"这个数据库.他的别名为"Database",本句可以写成"Database=Northwind".
                // "Server=YourSQLServer":使用名为"YourSQLServer"的服务器.他的别名为"Data Source","Address","Addr".
                // " Connect Timeout=30":连接超时时间为30秒.(根据情况添加)
                // PS:
                //  1.你的SQL Server必须已经设置了需要用户名和密码来登录,否则不能用这样的方式来登录.如果你的SQL Server设置为Windows登录,那么在这里就不需要使用"uid"和"password"这样的方式来登录,而需要使用"Trusted_Connection=SSPI"来进行登录.
                //  2. 如果使用的是本地数据库且定义了实例名,则可以写为"Server=(local)\实例名";如果是远程服务器,则将"(local)"替换为远程服务器的名称或IP地址.
                string strConnection = "Trusted_Connection=SSPI;";
                strConnection += "database=NTF_Navision_enlistment60;Server=CAIXIATA-6BE823;";
                strConnection += "Connect Timeout=30";
                using (SqlConnection objConnection = new SqlConnection(strConnection))
                {
                    objConnection.Open();
                    // method 1
                    SqlCommand myCommand = new SqlCommand("select * from Couse", objConnection);                Console.WriteLine("open");
                    SqlDataReader myReader = myCommand.ExecuteReader();
                    while (myReader.Read())
                    {
                        Console.WriteLine(myReader.GetFieldType(0));
                        Console.WriteLine(myReader.GetFieldType(1));
                        Console.WriteLine(myReader.GetFieldType(2));
                        Console.WriteLine(myReader.GetDecimal(0));
                        Console.WriteLine(myReader.GetDecimal(1));
                        Console.WriteLine(myReader.GetDecimal(2));
                    }
                    myReader.Close();                // method 2
                    SqlDataAdapter myCommandd = new SqlDataAdapter("select * from Couse", objConnection);
                    DataSet ds = new DataSet();
                    myCommandd.Fill(ds, "Couse");
                    DataTable dt = ds.Tables["couse"];
                    Console.WriteLine(dt.Columns[0].ToString());
                    Console.WriteLine(dt.DefaultView.Count);
                }
            }
        }
    }本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/LCL_data/archive/2009/04/30/4139145.aspx
      

  6.   

    string commandString = "select * from operator where Op_user='"+this.txtUser.Text.ToString()+"'";执行查询。string user_password = ds.Tables[0].Rows[0][口令字段的id].ToString(); //判断
    if (user_password.Trim() == txtpwd.text.toString())
    {}
      

  7.   


    H1:配置包含数据库连接详细资料的字符串string connectionString = 
        "server = Localhost;database = NorthWind;uid = sa;uid = sa;pwd = sa";步骤2:创建用于连接到该数据库的SqlConnection对象SqlConnection mySqlConnection = new SqlConnection(connectionString);步骤3:配置包含SELECT语句的字符串 string selectString =
    "select CustomerID,CompanyName,ContactName,Address " +
    "from Customers " +
    "where CustomerID < 'BSBEV'";步骤4:创建保存SELECT语句的SqlConnection对象SqlCommand mySqlCommand = mySqlConnection.CreateCommand();步骤5:将SqlCommand对象的CommandText属性设置为SELECT字符串mySqlCommand.CommandText = selectString;步骤6:创建一个SqlDataAdapter对象SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();步骤7:将SqlAdapter对象的SelectCommand属性设置为SqlCommand对象mySqlDataAdapter.SelectCommand = mySqlCommand;步骤8:创建用于存储SELECT语句结果的DataSet对象DataSet myDataSet = new DataSet();步骤9:利用SqlConnection对象的Open()方法打开数据库连接mySqlConnection.Open();步骤10:调用SqlDataAdapter对象的Fill()方法检索表行string dataTableName = "Customers";
    mySqlDataAdapter.Fill(myDataSet,dataTableName);步骤11:从DataSet对象中获取DataTable对象DataTable myDataTable = myDataSet.Tables[dataTableName];步骤12:显示DataTable中的每一行的列foreach(DataRow myDataRow in myDataTable.Rows)
    {
    //....
    }步骤13:关闭数据库连接 mySqlConnection.Close();