sqlcommand

SqlCommand (Using Statement / Disposing issue)

可紊 提交于 2020-01-01 04:45:10
问题 Take the following example... Using cn As New SqlConnection(ConnectionString) Try Dim cmd As SqlCommand = New SqlCommand With cmd .Connection = cn .Connection.Open() .CommandText = "dbo.GetCustomerByID" .CommandType = CommandType.StoredProcedure .Parameters.Add("@CustomerID", SqlDbType.Int, 4) .Parameters("@CustomerID").Value = CustomerID End With da = New SqlDataAdapter(cmd) da.Fill(ds, "Customer") Catch ex As Exception End Try End Using From my research today is sounds as though this is

Convert SqlCommand Output to List<MyType>?

徘徊边缘 提交于 2019-12-30 11:22:09
问题 I am using an ADO.NET SqlCommand with a single SqlDbType.Structured parameter to send a table-valued parameter to a sproc. The sproc returns many rows, which I need to get into a strongly-Typed List of . What is the best way to convert the result set (whether DataTable from a DataAdapter or DataReader bits) into List? Thanks. 回答1: You can use LINQ with a DataReader: var list = reader.Cast<IDataRecord>() .Select(dr => new YourType { Name = dr.GetString(0), ... }) .ToList(); 回答2: The most

c# execute SqlCommand with Parameters in “using” code block

不问归期 提交于 2019-12-29 09:02:06
问题 I am attempting to execute an SQL Command through a 'using' code block, but can't seem to get it to work with Parameters. I get the error: 'Parameters does not exist in the current context', does anyone have a possible solution for this problem? Heres My code: DataTable dt = new DataTable(); using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) using (var cmd = new SqlCommand(" SELECT FName" + " FROM EmployeeTable " + " WHERE

How can I Pass a Table Name to SqlCommand?

不想你离开。 提交于 2019-12-29 07:59:48
问题 I am trying to pass a table name as a parameter to my query through SqlCommand but it doesn't seems to be working. Here is my code; SqlConnection con = new SqlConnection( "server=.;user=sa;password=12345;database=employee" ); con.Open( ); SqlCommand cmd = new SqlCommand( "drop table @tbName" , con ); cmd.Parameters.AddWithValue( "@tbName" , "SampleTable" ); cmd.ExecuteNonQuery( ); con.Close( ); 回答1: SqlCommand.Parameters are supported for Data manipulation language operations not Data

Execute Oracle Function from SSIS OLE DB Command

99封情书 提交于 2019-12-25 12:13:31
问题 I am trying to call a function from oracle using OLE DB Command for SSIS, I have the connection set up Correctly but I think my Syntax for calling the function is incorrect? EXEC UPDATE_PERSON ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? output I have used this on a SQL stored proc for testing & it has worked. the oracal connection is 3rd party & they have just supplied the function name & expected parameters. I should get 1 return parameter. The Error: Error at Data Flow Task [OLE DB

C# abstraction and database layer

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 09:50:57
问题 I am wondering what's a better way to abstract some of this code, into a simple DAL. At this time, I'm just patching the code and don't have time or need yet to use EF, Linq2Sql or any ORM right now. public string GetMySpecId(string dataId) { using (SqlConnection conn = new SqlConnection(this.MyConnectionString)) { conn.Open(); // Declare the parameter in the query string using (SqlCommand command = new SqlCommand(@"select ""specId"" from ""MyTable"" where ""dataId"" = :dataId", conn)) { //

How to set an if on a select for returning select in sql?

心不动则不痛 提交于 2019-12-25 09:26:28
问题 I have two tables named User and ParentUser that there is an one to many relation between them and the many side is the ParentUser table. I have written this select on User table: select u.* from [User] u inner join ParentUser p on u.UserId=p.UserId where p.ParentId=2440 Now I wanna add another column to u.* containing 0 or 1.0 is for users who have some children in ParentUser and 1 is for those whom dont have any. How to handle this? Update 回答1: You need LEFT JOIN SELECT u.*, CASE WHEN p

asp.net multiple sqlcommands in one GridView

若如初见. 提交于 2019-12-25 04:33:14
问题 I have problem to show multiple sql command in one GridView. Maybe I don't need two sqlcommands to show from two tables but I don't know how to do. The first command is to get all employees that have vacation between two dates. The second command I am using it to retrieve dates by ID. But I don't know how to Bind them both to one GridView to show as attached image. Thank you in advance. What I get Now is Albert 2016-03-16 Albert 2016-03-17 Albert 2016-03-18 Johanna 2016-03-17 Johanna 2016-03

How do I delete all tables in a database using SqlCommand?

孤人 提交于 2019-12-24 08:57:10
问题 Unlike the other posts about the task "delete all tables", this question is specifically about using SqlCommand to access the database. A coworker is facing the problem that no matter how it attempts it, he can't delete all tables from a database via SqlCommand. He states that he can't perform such action as long as there is an active connection - the connection by the SqlCommand itself. I believe this should be possible and easy, but I don't have much of a clue about databases so I came here

Executing merge statement via C# SqlCommand not working

寵の児 提交于 2019-12-23 21:56:52
问题 I am making my first attempt at using a temp table and a MERGE statement to update a SQL table via a SqlCommand object in C#. The program I'm working on is designed to first export a very large set of records (upwards of 20k+) into an excel spreadsheet. The user then has the ability to do a search and replace for a specific value and update as many fields in as many records as they like. What I'm trying to do is then take that spreadsheet, populate a DataTable with it, and then populate a