none
关于数据库初始化的问题 RRS feed

  • 问题

  •       首先我是一名学习c#的小白。。。不要笑话我,我估计很多问题都是属于白痴级别的,但是没有恰当的书籍可以借阅,只得求助大神相助了。。

    我自己做了一个登录界面,需要用户名和密码,而这些东西存储在access数据库里面,初始化我只用了

     public partial class LoadForm : Form
        {

            SqlConnection conn = new SqlConnection();

    对于密码和用户值的变量是这样的

    static string user = "select * from t_counter  where User= ";
            static string pass = "select * from t_counter  where Password= ";

    但是

                   SqlCommand cmd = new SqlCommand(string.Format("select count(*) from t_counter where User='{0}' and Password='{1}'", user, pass), conn);
                    try
                    {
                        conn.Open();//·········打开连接-》这一步总是提示错误(ConnectionString 属性尚未初始化)
                        int count = (int)cmd.ExecuteScalar();                   

                       if (count > 0)

                        {
                            FrmMain form1 = new FrmMain();
                            form1.Show();     
                        }
                        else
                        {
                            MessageBox.Show("用户名或密码错误!!");
                        }

    该如何解决啊大神们

    2014年4月29日 0:42

答案

  • ConnectionString是连接字符串,你的SqlConnection conn = new SqlConnection();没有连接字符串,

    在你打出SqlConnection conn = new SqlConnection(  的时候会有提示器的

    2014年4月29日 1:28
  • 你好:

    SqlConnection是针对Sql Server数据库的,连接Access数据库的话可以使用OleDbConnection。需要给OleDbConnection实例传递一个数据库的链接字符串,对于Access2007以上版本可以使用如下数据库连接字符串:

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\test.accdb;Persist Security Info=False;
    string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\test.accdb;Persist Security Info=False;";
                string sql = "select * from t_test";
                using (OleDbConnection connection = new OleDbConnection(connectionString))
                {
                    connection.Open();
                    using (OleDbCommand command = new OleDbCommand(sql, connection))
                    {
                        DataSet ds = new DataSet();
                        OleDbDataAdapter adapter = new OleDbDataAdapter(command);
                        adapter.Fill(ds);
                    }
                }



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.





    2014年4月30日 3:06
    版主
  • 1L说的对,ConnectionString是连接字符串,所以在SqlConnection conn =new SqlConnection()之前应该定义ConnectionString,

    例如  string Connect = @" Provider = Microsoft.Jet.OLEDB.4.0;
               Data Source =D:\Documents\Visual Studio 2008\Projects\FinalDisign\FinalDisign\Database1.mdb";

    然后再 OleDbConnection ODBCon = new OleDbConnection(Connect);

    我连接的是access数据库,sql用的是Sqlconnection

    2014年5月5日 13:17

全部回复

  • ConnectionString是连接字符串,你的SqlConnection conn = new SqlConnection();没有连接字符串,

    在你打出SqlConnection conn = new SqlConnection(  的时候会有提示器的

    2014年4月29日 1:28
  • 你好:

    SqlConnection是针对Sql Server数据库的,连接Access数据库的话可以使用OleDbConnection。需要给OleDbConnection实例传递一个数据库的链接字符串,对于Access2007以上版本可以使用如下数据库连接字符串:

    Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\test.accdb;Persist Security Info=False;
    string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\test.accdb;Persist Security Info=False;";
                string sql = "select * from t_test";
                using (OleDbConnection connection = new OleDbConnection(connectionString))
                {
                    connection.Open();
                    using (OleDbCommand command = new OleDbCommand(sql, connection))
                    {
                        DataSet ds = new DataSet();
                        OleDbDataAdapter adapter = new OleDbDataAdapter(command);
                        adapter.Fill(ds);
                    }
                }



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.





    2014年4月30日 3:06
    版主
  • 1L说的对,ConnectionString是连接字符串,所以在SqlConnection conn =new SqlConnection()之前应该定义ConnectionString,

    例如  string Connect = @" Provider = Microsoft.Jet.OLEDB.4.0;
               Data Source =D:\Documents\Visual Studio 2008\Projects\FinalDisign\FinalDisign\Database1.mdb";

    然后再 OleDbConnection ODBCon = new OleDbConnection(Connect);

    我连接的是access数据库,sql用的是Sqlconnection

    2014年5月5日 13:17