sqlcommand

How should I pass a user-defined type to SqlParameterCollection.AddWithValue?

此生再无相见时 提交于 2019-12-11 05:05:02
问题 I have a custom data type called StudentID , which has an implicit conversion to string. When I pass a StudentID instance to SqlCommand.Parameters.AddWithValue (as the value) and execute the command, I receive the following error: No mapping exists from object type StudentID to a known managed provider native type. Specifying a type for the parameter like SqlDbType.NVarChar doesn't help. The only thing that works is to explicitly cast the StudentID value to a string, which defeats the purpose

How to write a query to check if a child has any children in a table

痴心易碎 提交于 2019-12-11 04:26:13
问题 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 wanna write a query to pass a parentId and fetch all of its children and a column name HasChildren to see whether every child has any children or not. The picture below shows some sample data and needed result: Tries: 1-By Prdp SELECT u.*, CASE WHEN p.ParentId IS NULL THEN 1 ELSE 0 END as HasChildren FROM [User] u LEFT JOIN (select distinct ParentId from

Mysql change column name from “group” to “group_code”

ぐ巨炮叔叔 提交于 2019-12-11 03:32:47
问题 I have set a column name to "group", which turned out to be a reserved word. Now I try to change the name to "group_code", but I get an error. I try: ALTER TABLE task_values CHANGE group group_code VARCHAR(40) NOT NULL; and ALTER TABLE task_values CHANGE 'group' group_code VARCHAR(40) NOT NULL; but both fail, I get "No such element" error. Please help 回答1: You will need to use backticks around group which, as you said, is a mysql reserved keyword ALTER TABLE task_values CHANGE `group` group

How to add parameter in SELECT query for fieldName IN @fieldName construction [duplicate]

给你一囗甜甜゛ 提交于 2019-12-11 02:36:21
问题 This question already has answers here : Pass Array Parameter in SqlCommand (11 answers) Closed 4 years ago . string idVariable = "qwerty"; string sqlQuery = "select id from user where id = @id"; sqlCommand.Parameters.Add("@id", SqlDbType.VarChar).Value = idVariable; Adding a value for a specific field is ok. What if I need to have a few ids and IN in WHERE clause? List<string> ids = new List<string>{"qwe", "asd", "zxc"}; string sqlQuery = "select id from user where id IN @ids"; sqlCommand

My stored procedure returns something, but sqlDataAdapter.Fill leaves datatable empty

こ雲淡風輕ζ 提交于 2019-12-11 00:39:38
问题 Something stopped working in my application. I tested my stored procedure and it does return 3 rows (too big to repost here). At the output in Visual Studio it says afterwards: (1 row(s) affected) (3 row(s) returned) @RETURN_VALUE = 0 Finished running [dbo].[GetCaseDocumentFile]. Why can't I put it into the datatable called dtResult? The command sqlDataAdapter.Fill(dtResult) should do it. Instead I get {} when looking at it in debug mode. string connectionString = @"Data Source=34.56.55.251

Why does ExecuteReader() pad strings with traling spaces?

試著忘記壹切 提交于 2019-12-11 00:17:39
问题 I have two queries: Q1: select name from presidents where country = 'USA' Q2: select 'Obama' as name from presidents where country = 'USA' When using System.Data.Odbc.OdbcCommandExecuteReader then the returned DbDataReader contains 'Obama' in case of Q1 and 'Obama ' in case of Q2. Why is the string padded with trailing spaces in case of Q2 and what is the remedy? Trimming is ugly and even wrong in some cases. I am using .Net Framework 3.5. Here is the test code OdbcCommand cmd = new

Why does a SqlException thrown by SqlCommand.ExecuteNonQuery contain all the PRINTs as errors?

 ̄綄美尐妖づ 提交于 2019-12-10 19:25:04
问题 When I run the following snippet try { using (SqlConnection conn = new SqlConnection("I'm shy")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "PRINT 'A';PRINT 'B';PRINT 'C';RAISERROR('SQL_Error', 18, 1)"; cmd.ExecuteNonQuery(); } } } catch (SqlException ex) { MessageBox.Show(ex.Message); } I get the following message: SQL_Error A B C and ex.Errors has 4 entries (The 3 SqlError 's corresponding to the prints have a SqlError.Class of 0 (vs. 18 for the real

Set custom default CommandTimeout for all new Command Objects

佐手、 提交于 2019-12-10 12:31:13
问题 The default CommandTimeout value is 30 seconds. You can manually change the value on an instance of the command object by doing the following Dim cmd As New System.Data.SqlClient.SqlCommand cmd.CommandTimeout = 60 Is there a way to specify a different default value, such that all new command objects will automatically have this value within your solution when they are created? 回答1: As far as I know no, there is no way to change this default. The constructor code for a SqlCommand is this:

Confused between SqlCommand & SqlDataAdapter

前提是你 提交于 2019-12-09 05:29:26
问题 everyone I am a student and new to .NET and specially MVC3 development but for one of my project I’ve to work over it and so going through the learning phase Issue and confusion I am facing is regarding DB-Connectivity, whast I leanree d regarding retrieving records from a database is something like this: //Method One: var conn = new SqlConnection(conString.ConnectionString); const string cmdString = "Select * FROM table"; var cmd = new SqlCommand(cmdString, conn); var mySqlDataAdapter = new

Should you reuse SqlConnection, SqlDataAdapter, and SqlCommand objects?

空扰寡人 提交于 2019-12-08 19:46:12
问题 I'm working with a DAL object that is written in a layout similar to the following code. I simplified a lot of the code code just to show the setup. public class UserDatabase : IDisposable { private SqlDataAdapter UserDbAdapter; private SqlCommand UserSelectCommand; private SqlCommand UserInsertCommand; private SqlCommand UserUpdateCommand; private SqlCommand UserDeleteCommand; private System.Data.SqlClient.SqlConnection SQLConnection; public UserDatabase() { this.SQLConnection = new System