高手进,菜鸟遇到了个菜鸟问题,跪求!没分了。。。顶下吧。。。 两种没有什么区别,但最好try{}catch{}finally{}包含起来.. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using (SqlConnection connection = new SqlConnection(connectionString)) { using (SqlCommand cmd = new SqlCommand(SQLString,connection)) { try { connection.Open(); int rows=cmd.ExecuteNonQuery(); return rows; } catch(System.Data.SqlClient.SqlException E) { connection.Close(); throw new Exception(E.Message); } finally { connection.Close(); } } } ========第一种 SQLConnection Conn= new SqlConnection(); Conn.Open();SqlCommand cmd=new SqlCommand();cmd.ExcuteQuery();Conn.Close();===================很明显 有问题 : cmd 没有与 Conn 关联起来..,Conn 没有打开楼主 应该表达的是 通过 SQL 字符串来执行操作..要么跟第二中一样用cmd.Connection=Conn;cmd.CommandText=query;要么用 SqlCommand cmd=new SqlCommand(Conn,query);==第二种SqlCommand sqlCmd = new SqlCommand();sqlCmd.Connection = DataQuery.GetNewSqlConnection(); ====ConnsqlCmd.CommandText = query; ===代表SQL 字符串sqlCmd.CommandType = CommandType.Text;=======他有3个参数,默认就是这个 if(sqlCmd.Connection.State == System.Data.ConnectionState.Closed)sqlCmd.Connection.Open();intResult = sqlCmd.ExecuteNonQuery(); sqlCmd.Connection.Close();sqlCmd.Dispose();sqlCmd = null;========================================= SQLConnection Conn= new SqlConnection("-------------");Conn.Open();SqlCommand cmd=new SqlCommand("--------------",Conn);cmd.ExcuteQuery();===================================此步之前应该还有别的语句Conn.Close();第二种很少人这样写的吧 请问SqlParameter.SourceColumn属性,这里怎么解释? 不用new作为修饰符怎么也没问题。。 如何用泛型在EventHandler委托中声明自定义事件数据? 客户端执行VB程序需要.netfromwork框架吗 如果让你设计编程语言 请问相对路径怎么写? 三层问题 打开新的windows窗体. 大家不要笑 :) 赋值时==表示什么?不太明 [c#]请问如何隐式Get提交后获取到对方返回的XML 有没有西安的吗?我初学C#,想找个人带带:) 明天面试,求救!
{
using (SqlCommand cmd = new SqlCommand(SQLString,connection))
{
try
{
connection.Open();
int rows=cmd.ExecuteNonQuery();
return rows;
}
catch(System.Data.SqlClient.SqlException E)
{
connection.Close();
throw new Exception(E.Message);
}
finally
{
connection.Close();
}
}
}
第一种
SQLConnection Conn= new SqlConnection();
Conn.Open();
SqlCommand cmd=new SqlCommand();
cmd.ExcuteQuery();
Conn.Close();===================很明显 有问题 : cmd 没有与 Conn 关联起来..,Conn 没有打开
楼主 应该表达的是 通过 SQL 字符串来执行操作..
要么跟第二中一样用cmd.Connection=Conn;
cmd.CommandText=query;
要么用 SqlCommand cmd=new SqlCommand(Conn,query);
==
第二种
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = DataQuery.GetNewSqlConnection(); ====Conn
sqlCmd.CommandText = query; ===代表SQL 字符串
sqlCmd.CommandType = CommandType.Text;=======他有3个参数,默认就是这个
if(sqlCmd.Connection.State == System.Data.ConnectionState.Closed)
sqlCmd.Connection.Open();
intResult = sqlCmd.ExecuteNonQuery();
sqlCmd.Connection.Close();
sqlCmd.Dispose();
sqlCmd = null;
=========================================
Conn.Open();
SqlCommand cmd=new SqlCommand("--------------",Conn);
cmd.ExcuteQuery();===================================此步之前应该还有别的语句
Conn.Close();第二种很少人这样写的吧