请教下DATAGRID这个呢??我怎么什么数据也没有呢??本来不是可以连接我的表么????
是webform的吗?在web.config文件里,</system.web>和</configuration>之间,加入:
<appSettings> <add key="sqlconnectionstring" value="server=服务器名或IP地址;database=数据库名;uid=用户名;password=密码" /> </appSettings>
然后把数据检索到DataGrid时,就可以用这个,红色那段是读上面的个数据库连接字串的:
//打开库 SqlConnection MyConnection=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["sqlconnectionstring"]); MyConnection.Open(); SqlCommand MyCommand=new SqlCommand(mysql,MyConnection); SqlDataReader dr=MyCommand.ExecuteReader(); DataGrid1.DataSource=dr; DataGrid1.DataBind(); dr.Close(); MyConnection.Close(); 请教下,不用弄什么控件了么?????这样就能显示数据库里面的内容么???????????? 怎么我弄了,不能呢????是不是也要控件SQLDATAADATER和SQLCONN还有DATASET和DATAVIEW几个呢一起用呢?怎么让内容在DATAGRID上显示呢????????而且要有选择的字段显示~~~~~~ 还有,请教下为什么我在注册的时候,提示注册成功,可数据库里面没写进去呢????
private void Button1_Click(object sender, System.EventArgs e) { if(Page.IsValid) { if(UserValid()) { SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["sqlconnectionstring"]); conn.Open();
string sql = "Insert into Users(UserName,Password,Email) values('" + this.UserName.Text + "','" + this.Password.Text + "','" + this.email.Text + "')"; SqlCommand cmd = new SqlCommand(sql,conn); try { cmd.ExecuteNonQuery(); } catch { } finally { cmd.Dispose(); conn.Close(); } this.Panel1.Visible = false; this.Panel2.Visible = true; } else { this.Label1.Visible = true; } } } private bool UserValid() { SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["sqlconnectionstring"]); conn.Open(); string sql = "select * from Users where UserName = '" + this.UserName.Text + "'"; SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataReader reader = cmd.ExecuteReader(); if(reader.Read()) { return false; } else { return true; } } 这些是我提交的注册信息,是不是没写进数据库呢??????/