private void Buttom1_Click(object sender, System.EventArgs e)///////////Buttom事件,留言按扭
{
SqlConnection mycn = null; if(Name.Text ==""||Tel.Text==""||Title.Text==""||Text.Text=="" )
{
Label1.Text="所有的都不能为空,请检查!";
return;
}
else
{
try
{
string myConnection = "server=local;uid=sa;pwd=fjkdsla;database=TestSun";
string sql = " insert into Tools( Name,Mail,Title,Text ) values ('"+Name.Text+"','"+Tel.Text+"','"+Title.Text+"','"+Text.Text+"') ";
SqlConnection mycn = new SqlConnection ( myConnection );
mycn.Open();
SqlCommand mysql = new SqlCommand( sql,mycn );
mysql.ExecuteNonQuery();
////////////////////// mycn.Close();
Label1.Text = "";
Name.Text = "";
Tel.Text = "";
Title.Text = "";
Text.Text = "";
}
catch
{
Label1.Text="数据库连接出错,请检查!";
}
finally
{
if( mycn != null )
{
mycn.Close();
}
}
}
}以上程序,上面已经定义SqlConnection mycn = null;为什么SqlConnection mycn = new SqlConnection ( myConnection );就不通定义呢,而下面那段程序就可以的
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection mycn = null;
try
{
string myConnection = "server=local; uid =sa ;pwd =fjkdsla; database=TestSun";
string sql = "select * from books";
mycn = new SqlConnection(myConnection)
mycn.Open();
SqlCommand mysql = new SqlCommand(sql,mycn);
rep.DataSource = mysql.ExecuteReader();
rep.DataBind();
}
catch
{
Label1.Text="数据库连接出错,请检查!";
}
finally
{
if (mycn != null)
{
mycn.Close();
}
}
}
{
SqlConnection mycn = null; if(Name.Text ==""||Tel.Text==""||Title.Text==""||Text.Text=="" )
{
Label1.Text="所有的都不能为空,请检查!";
return;
}
else
{
try
{
string myConnection = "server=local;uid=sa;pwd=fjkdsla;database=TestSun";
string sql = " insert into Tools( Name,Mail,Title,Text ) values ('"+Name.Text+"','"+Tel.Text+"','"+Title.Text+"','"+Text.Text+"') ";
SqlConnection mycn = new SqlConnection ( myConnection );
mycn.Open();
SqlCommand mysql = new SqlCommand( sql,mycn );
mysql.ExecuteNonQuery();
////////////////////// mycn.Close();
Label1.Text = "";
Name.Text = "";
Tel.Text = "";
Title.Text = "";
Text.Text = "";
}
catch
{
Label1.Text="数据库连接出错,请检查!";
}
finally
{
if( mycn != null )
{
mycn.Close();
}
}
}
}以上程序,上面已经定义SqlConnection mycn = null;为什么SqlConnection mycn = new SqlConnection ( myConnection );就不通定义呢,而下面那段程序就可以的
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection mycn = null;
try
{
string myConnection = "server=local; uid =sa ;pwd =fjkdsla; database=TestSun";
string sql = "select * from books";
mycn = new SqlConnection(myConnection)
mycn.Open();
SqlCommand mysql = new SqlCommand(sql,mycn);
rep.DataSource = mysql.ExecuteReader();
rep.DataBind();
}
catch
{
Label1.Text="数据库连接出错,请检查!";
}
finally
{
if (mycn != null)
{
mycn.Close();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货