parameterized-query

Import Excel .xslx file into sql server : difficulty in updating table

泄露秘密 提交于 2019-11-30 21:04:11
问题 I am importing excel file into sql server datatbase. The code works fine but the way I am doing currently is deleting (clear the table) the table data. string ssqltable = "tStudent"; string myexceldataquery = "select id,student,rollno,course from [sheet1$]"; try { string sexcelconnectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" + excelfilepath + "; Extended Properties=\"Excel 12.0; HDR=Yes; IMEX=2\""; string ssqlconnectionstring = "Data Source=DELL\\SQLSERVER1;Trusted

SQLServerCE Problem with parameterized queries from .NET

*爱你&永不变心* 提交于 2019-11-29 15:45:55
I am pulling the hair out of my head trying to figure this one out. I can't make Parameterized queries to work in VB.Net, when I am using parameters. From what I have found, using a parameter in a function, from .NET raises an error (see sample code). However, running the not working query in the Query Window in Visual studio works properly. The error raised is: 25922 - The arguments for function are not valid. Info from: http://msdn.microsoft.com/en-us/library/aa256772%28SQL.80%29.aspx Sample Code: Imports System.Data.SqlServerCe Public Class MiniDemo Public Shared Sub Main() Dim cs As String

How to test my ad-hoc SQL with parameters in Postgres query window

て烟熏妆下的殇ゞ 提交于 2019-11-29 10:54:26
In Microsoft SQL Server, to test something like this in the query window: select * from Users where LastName = @lastname I can add this before the command: declare @lastname varchar(16) set @lastname = 'Troy' But in PostgreSQL, I cannot find a similar way to do so. It seems the only thing I can do is to replace the parameter name directly with its value. It gets hard when the ad-hoc query gets complicated and the same parameter gets used several times. Is there a way? Various options. Provide parameters in a CTE to have "variables" in pure SQL : WITH var(lastname) AS (SELECT 'Troy'::varchar(16

“Must declare the variable @myvariable” error with ADO parameterized query

送分小仙女□ 提交于 2019-11-29 07:56:04
i am trying to use parameterized queries with ADO. Executing the Command object throws the error: Must declare the variable '@filename' i declare the parameter @filename using CreateParameter/Append : sql := 'INSERT INTO Sqm(Filename, data) VALUES(@filename, @data)'; command := CoCommand.Create; command.Set_ActiveConnection(Connection.ConnectionObject); command.Set_CommandText(sql); command.Set_CommandType(adCmdText); command.Parameters.Append(Command.CreateParameter('@filename', adLongVarWChar, adParamInput, -1, Filename)); command.Parameters.Append(Command.CreateParameter('@data',

ExecuteNonQuery inside loop

我只是一个虾纸丫 提交于 2019-11-29 07:04:25
I'm trying to insert a database record inside a loop in C#. It works when I hard code the values like this: string query3 = "INSERT INTO furniture (room_id,member_id) VALUES (222,333);"; SqlCommand cmd3 = new SqlCommand(query3, sqlConnection3); sqlConnection3.Open(); for (int i = 0; i < arrItemsPlanner.Length; i++) { try { cmd3.ExecuteNonQuery(); } catch { return "Error: Item could not be saved"; } finally { //Fail } } But when I use parameterised queries it doesn't work - even if I hard code a value into the parameterised query like this: string query3 = "INSERT INTO furniture (room_id,member

Bulk Parameterized Inserts

风格不统一 提交于 2019-11-29 06:23:38
I'm trying to switch some hard-coded queries to use parameterized inputs, but I've run into a problem: How do you format the input for parameterized bulk inserts? Currently, the code looks like this: $data_insert = "INSERT INTO my_table (field1, field2, field3) "; $multiple_inserts = false; while ($my_condition) { if ($multiple_inserts) { $data_insert .= " UNION ALL "; } $data_insert .= " SELECT myvalue1, myvalue2, myvalue3 "; } $recordset = sqlsrv_query($my_connection, $data_insert); A potential solution (modified from How to insert an array into a single MySQL Prepared statement w/ PHP and

The way PDO parametrized query works

允我心安 提交于 2019-11-29 04:19:27
PLEASE READ THE QUESTION CAREFULLY. It is not usual silly "my code doesn't work!!!" question. When I run this code with intended error try { $sth = $dbh->prepare("SELECT id FROM users WHERE name INN(?,?) "); $sth->execute(array("I'm","d'Artagnan")); } catch (PDOException $e) { echo $e->getMessage(); } I get this error message You have an error in your SQL syntax ... near 'INN('I\'m','d\'Artagnan')' at line 1 But I thought for years that query and data being sent to the server separately and never interfere. Thus I have some questions (though I doubt anyone got an answer...) Where does it get

How to insert null value in Database through parameterized query

杀马特。学长 韩版系。学妹 提交于 2019-11-29 01:30:26
I have a datetime datatype : dttm Also the database field type is datatime Now I am doing this: if (dttm.HasValue) { cmd.Parameters.AddWithValue("@dtb", dttm); } else { // It should insert null value into database // through cmd.Parameters.AddWithValue("@dtb", _____) } How can this be done. Use DBNull.Value if (dttm.HasValue) { cmd.Parameters.AddWithValue("@dtb", dttm); } else { cmd.Parameters.AddWithValue("@dtb", DBNull.Value) } This can be done using the null-coalescing operator: if the value of dttm is null the DBNull.Value will be inserted otherwise the value of dttm will be used cmd

Querying a SQL Server in Excel with a parameterized query using VBA

空扰寡人 提交于 2019-11-28 13:00:45
I'm trying to query a table in Microsoft Excel using VBA. I've written up some code to try and accomplish this task, but I keep getting a run-time error '1004' saying it's a General ODBC error. I'm not sure what I need to do to get this code to run properly so I can query this table. I'm using SQL Server Express, the server I'm connecting to: .\SQLEXPRESS Database: Databaselink Querying the products table VBA Code: Sub ParameterQueryExample() '---creates a ListObject-QueryTable on Sheet1 that uses the value in ' Cell Z1 as the ProductID Parameter for an SQL Query ' Once created, the query will

How do you specify 'DEFAULT' as a SQL parameter value in ADO.NET?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 10:54:45
I have a parameterized SQL query targetted for SQL2005 which is dynamically created in code, so I used the ADO.NET SqlParameter class to add sql parameters to SqlCommand . In the aforementioned SQL I select from a Table Valued Function with has defaults. I want my dynamic sql to sometimes specify a value for these default parameters, and other times I want to specify that the SQL DEFAULT - as defined in the Table Valued Function - should be used. To keep the code clean I didn't want to dynamically add the SQL DEFAULT keyword and parameterize it when a non-default is to be used, I just wanted