using System.Data.SqlClient;
using System.Data;
......
public ..{
.
.
.SqlConnection cnnHouse = new SqlConnection(Application["ConnString"].ToString());
SqlCommand cmdHouse = new SqlCommand();
cmdHouse.CommandText = "SELECT col_name,col_text,flag FROM wlba_regContract_model where id= " + m_modid + " and userid = " + m_userid;
cmdHouse.Connection = cnnHouse;
cnnHouse.Open();
.
.
.}
为何通不过, 我的web.config文件也配置过了。以下的就通过(两种有什么区别:):
string strcon;
ConnectionStringSettingsCollection connectionStrings = WebConfigurationManager.ConnectionStrings as ConnectionStringSettingsCollection;
strcon = connectionStrings["ConnectionString"].ToString();
SqlConnection mysql = new SqlConnection(strcon);
mysql.Open();
SqlCommand mytable = new SqlCommand("select top 10 * from gulou",mysql);
SqlDataReader myread = mytable.ExecuteReader();
myread.Read();
String name;
name = myread.GetValue(5).ToString().Trim();
TextBox1.Text = name;
myread.Close();
mytable.Dispose();
mysql.Close();请指教
using System.Data;
......
public ..{
.
.
.SqlConnection cnnHouse = new SqlConnection(Application["ConnString"].ToString());
SqlCommand cmdHouse = new SqlCommand();
cmdHouse.CommandText = "SELECT col_name,col_text,flag FROM wlba_regContract_model where id= " + m_modid + " and userid = " + m_userid;
cmdHouse.Connection = cnnHouse;
cnnHouse.Open();
.
.
.}
为何通不过, 我的web.config文件也配置过了。以下的就通过(两种有什么区别:):
string strcon;
ConnectionStringSettingsCollection connectionStrings = WebConfigurationManager.ConnectionStrings as ConnectionStringSettingsCollection;
strcon = connectionStrings["ConnectionString"].ToString();
SqlConnection mysql = new SqlConnection(strcon);
mysql.Open();
SqlCommand mytable = new SqlCommand("select top 10 * from gulou",mysql);
SqlDataReader myread = mytable.ExecuteReader();
myread.Read();
String name;
name = myread.GetValue(5).ToString().Trim();
TextBox1.Text = name;
myread.Close();
mytable.Dispose();
mysql.Close();请指教
如果你觉得麻烦,笔者愿意提供捷径:private SqlConnection conn()//自定义函数,创建数据库连接
{
string ConnStr="server=.;database=myMail;uid=sa;pwd=;";
SqlConnection myConn= new SqlConnection(ConnStr);
return myConn;
}private void BindData()
{
SqlConnection myConnection=conn();//连接数据库
string queryStr="select * from MailBoxInformation order by MailID DESC";
SqlDataAdapter myDataAdapter=new SqlDataAdapter(queryStr,myConnection);
DataSet myDataSet=new DataSet();
myDataAdapter.Fill(myDataSet,"MailBoxInformation");
//如果有数据,显示
if(myDataSet.Tables["MailBoxInformation"].Rows.Count>0)
{
DataGrid1.DataSource=myDataSet.Tables["MailBoxInformation"].DefaultView;
DataGrid1.DataBind();
}
else//如果没有,控件不可见
{
DataGrid1.Visible=false;
}
}
SqlConnection cnnHouse = new SqlConnection(Application["ConnString"].ToString()+"");
SqlConnection cnnHouse = new SqlConnection(Application["ConnString"]+"");
将ToString()去掉直接+""
1、要在web.config中的appSettings中设置。事下:<appSettings>
<add key="ConnectionString" value=" Data Source=YELIYU\YELIYU;Initial Catalog=yeliyu;User ID=sa;pwd=sa"/>
</appSettings>如是这连法可以按如下连接(注意,这和写法要创建一个全局的global.asax,后在application_start事件中增加:Application["connection"] = ConfigurationSettings.AppSettings["ConnectionString"].ToString();):(我举例说明:)SqlConnection strconn = new SqlConnection(Application["connection"].ToString());
SqlCommand mycomm = new SqlCommand();
mycomm.CommandText = "select * from login where userid="+"'"+TextBox1.Text+"'";
mycomm.Connection=strconn;
strconn.Open();
try
{
SqlDataReader myrd = mycomm.ExecuteReader();
if (myrd.HasRows)
{ myrd.Read();
TextBox2.Text= myrd.GetValue(System.Convert.ToInt16( myrd.GetOrdinal("userid"))).ToString().Trim();
}
}
catch (System.DivideByZeroException ex)
{
Console.WriteLine("数据库连接失败", ex); }2、直接连接web.config(举例如下:)
记得要增加命名空间:using System.Web.Configuration;ConnectionStringSettingsCollection connectionStrings = WebConfigurationManager.ConnectionStrings as ConnectionStringSettingsCollection;
strcon = connectionStrings["ConnectionString"].ToString();
SqlConnection mysql = new SqlConnection(strcon);
mysql.Open();
SqlCommand mytable = new SqlCommand("select top 10 * from gulou",mysql);
SqlDataReader myread = mytable.ExecuteReader();
myread.Read();
String name;
name = myread.GetValue(5).ToString().Trim();
TextBox1.Text = name;
mytable.Dispose();
mysql.Close();
myread.Close();