SqlCommand.Connection 属性
2008-04-15 20:01
423 查看
SqlCommand.Connection 属性
获取或设置 SqlCommand 的此实例使用的 SqlConnection。[Visual Basic] Public Property Connection As SqlConnection [C#] public SqlConnection Connection {get; set;} [C++] public: __property SqlConnection* get_Connection(); public: __property void set_Connection(SqlConnection*); [JScript] public function get Connection() : SqlConnection; public function set Connection(SqlConnection);
属性值
与数据源的连接。默认值为空引用(Visual Basic 中为 Nothing)。异常
异常类型 | 条件 |
---|---|
InvalidOperationException | 在事务进行过程中更改了 Connection 属性。 |
备注
如果在事务进行过程中设置 Connection,而且 Transaction 属性不为空,则会生成 InvalidOperationException。如果 Transaction 属性不为空,而事务已经提交或回滚,则 Transaction 设置为空。示例
[Visual Basic, C#, C++] 下面的示例将创建一个 SqlCommand 并设置它的一些属性。[Visual Basic] Public Sub CreateMySqlCommand() Dim mySelectQuery As String = "SELECT * FROM Categories ORDER BY CategoryID" Dim myConnectString As String = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer" Dim myCommand As New SqlCommand(mySelectQuery) myCommand.Connection = New SqlConnection(myConnectString) myCommand.CommandTimeout = 15 myCommand.CommandType = CommandType.Text End Sub 'CreateMySqlCommand [C#] public void CreateMySqlCommand() { string mySelectQuery = "SELECT * FROM Categories ORDER BY CategoryID"; string myConnectString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer"; SqlCommand myCommand = new SqlCommand(mySelectQuery); myCommand.Connection = new SqlConnection(myConnectString); myCommand.CommandTimeout = 15; myCommand.CommandType = CommandType.Text; } [C++] public: void CreateMySqlCommand() { String* mySelectQuery = S"SELECT * FROM Categories ORDER BY CategoryID"; String* myConnectString = S"Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer"; SqlCommand* myCommand = new SqlCommand(mySelectQuery); myCommand->Connection = new SqlConnection(myConnectString); myCommand->CommandTimeout = 15; myCommand->CommandType = CommandType::Text; }
[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮
。
要求
平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版请参见
SqlCommand 类 | SqlCommand 成员 | System.Data.SqlClient 命名空间 | CommandText | CommandTimeout | CommandType相关文章推荐
- SqlConnection.ConnectionString 属性
- SqlDataAdapter SelectCommand,InsertCommand,UpdateCommand,DeleteCommand 属性
- Combining SqlConnection and SqlCommand in one Class in C#
- SqlCommand.Parameters 属性
- 关于DataAdapter的问题-----Fill: SelectCommand.Connection 属性尚未初始化。
- 整理MSDN中的SqlConnection.ConnectionString 属性
- 整理MSDN中的SqlConnection.ConnectionString 属性
- SqlConnection.ConnectionString 属性
- SqlConnection.ConnectionString属性
- 我想操作的是利用SqlDataAdapter的几个Command属性(InsertCommand,UpdateCommand,DeleteCommand)来更新数据库
- SqlConnection.ConnectionTimeout 与 SqlCommand.CommandTimeout
- 网上说什么访问SqlConnection.State属性会很慢,纯属误导
- About SqlCommand & SqlConnection Timeout expired
- SqlDataAdapter.UpdateCommand 属性(极易出错的地方)(转自:http://blog.csdn.net/tsapi/archive/2011/03/29/6286654.aspx)
- SqlConnection.ConnectionString 属性
- SqlConnection.ConnectionTimeout 与 SqlCommand.CommandTimeout 及SqlBulkCopy.BulkCopyTimeout属性
- 整理MSDN中的SqlConnection.ConnectionString 属性
- All SQL Server SqlConnection properties(SqlConnection 连接串的所有属性)
- [转]整理MSDN中的SqlConnection.ConnectionString 属性
- 整理MSDN中的SqlConnection.ConnectionString 属性