using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;namespace Example04
{
/// <summary>
/// Exo4_o4 的摘要说明。
/// </summary>
public class Exo4_o4 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblCommStr;
protected System.Web.UI.WebControls.Label lblFlag;
protected System.Web.UI.WebControls.Button btnInsert;
protected System.Web.UI.WebControls.Button btnDelete;
protected SqlConnection myConn=new SqlConnection(); public void CreateMyCommand(string myExecuteQuery,SqlConnection myConnection)
{
SqlCommand myCommand = new SqlCommand(myExecuteQuery, myConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
lblCommStr.Text="命令的CommandText属性值为: "+
myCommand.CommandText;
myConnection.Close();
}
private void Page_Load(object sender, System.EventArgs e)
{
string strConn="server=Localhost;uid=sa;pwd=123;database=Northwind";
myConnection.ConnectionString=strConn;
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnInsert.Click += new System.EventHandler(this.btnInsert_Click);
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btnInsert_Click(object sender, System.EventArgs e)
{
string myExecuteQuery1="insert Categories(CategoryName,Description) values('Test','test')";
CreateMyCommand(myExecuteQuery1,myConnection);
lblFlag.Text="成功插入了一条记录!";
} private void btnDelete_Click(object sender, System.EventArgs e)
{
string myExecuteQuery2="delete from Categories where CategoryName like 'Test'";
CreateMycommand(myExecuteQuery2,myConnection);
lblFlag.Text="成功删除了一条记录!";
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;namespace Example04
{
/// <summary>
/// Exo4_o4 的摘要说明。
/// </summary>
public class Exo4_o4 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblCommStr;
protected System.Web.UI.WebControls.Label lblFlag;
protected System.Web.UI.WebControls.Button btnInsert;
protected System.Web.UI.WebControls.Button btnDelete;
protected SqlConnection myConn=new SqlConnection(); public void CreateMyCommand(string myExecuteQuery,SqlConnection myConnection)
{
SqlCommand myCommand = new SqlCommand(myExecuteQuery, myConnection);
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
lblCommStr.Text="命令的CommandText属性值为: "+
myCommand.CommandText;
myConnection.Close();
}
private void Page_Load(object sender, System.EventArgs e)
{
string strConn="server=Localhost;uid=sa;pwd=123;database=Northwind";
myConnection.ConnectionString=strConn;
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnInsert.Click += new System.EventHandler(this.btnInsert_Click);
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btnInsert_Click(object sender, System.EventArgs e)
{
string myExecuteQuery1="insert Categories(CategoryName,Description) values('Test','test')";
CreateMyCommand(myExecuteQuery1,myConnection);
lblFlag.Text="成功插入了一条记录!";
} private void btnDelete_Click(object sender, System.EventArgs e)
{
string myExecuteQuery2="delete from Categories where CategoryName like 'Test'";
CreateMycommand(myExecuteQuery2,myConnection);
lblFlag.Text="成功删除了一条记录!";
}
}
}
myConnection.ConnectionString=strConn;这是从那引用过来的呀?
{
string strConn="server=Localhost;uid=sa;pwd=123;database=Northwind";
myConnection.ConnectionString=strConn;
}
定义是myConn
{
string strConn="server=Localhost;uid=sa;pwd=123;database=Northwind";
myConnection.ConnectionString=strConn;
}
你的myConnection在那里定义的
{
string strConn="server=Localhost;uid=sa;pwd=123;database=Northwind";
myConn = new SqlConnection(strConn);
}
myConn = new SqlConnection(strConn);
后
CreateMyCommand(myExecuteQuery1,myConnection);名称“myConnection”在类或命名空间中不存在啊
string myConn="pwd=;database=.......";
{
myConnectionString = "Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;";
}
SqlConnection myConnection = new SqlConnection(myConnectionString);
string myInsertQuery = "INSERT INTO Customers (CustomerID, CompanyName) Values('NWIND', 'Northwind Traders')";
SqlCommand myCommand = new SqlCommand(myInsertQuery);
myCommand.Connection = myConnection;
myConnection.Open();
myCommand.ExecuteNonQuery();
myCommand.Connection.Close();
使用的myConnection