executenonquery

ExecuteNonQuery() throws “Incorrect syntax near the keyword 'User'”

不羁岁月 提交于 2019-12-02 12:26:32
问题 I started learning C# last week and I'm now having trouble with an INSERT sql statement. I used the following code, then I tell you what happens. private void AddNewUserToDataBase(User user) { string commandText = "INSERT INTO User (firstName,lastName,adress,town,favoriteCurrency) " + "VALUES (@firstName,@lastName,@adress,@town,@favoriteCurrency)"; using (SqlConnection connection = new SqlConnection(global::Laboratoire1.Properties.Settings.Default.Database1ConnectionString)) { using

ExecuteNonQuery() for Insert

大憨熊 提交于 2019-12-02 12:03:53
问题 Can you please tell me what's wrong with this code? Do I need to use DataAdapter to insert into a table? I know the connectionString is ok, because I tested it on the Server Explorer. Dim mydao As New Connection Dim connectionString As String = mydao.GetConnectionString() Dim connection As New SqlConnection(connectionString) Dim cmd As New SqlCommand Public Function add(ByVal area As String, ByVal user As String) As Integer cmd.CommandText = "INSERT into Area (Area, user) VALUES ('" + area +

ExecuteNonQuery() throws “Incorrect syntax near the keyword 'User'”

倾然丶 夕夏残阳落幕 提交于 2019-12-02 05:46:18
I started learning C# last week and I'm now having trouble with an INSERT sql statement. I used the following code, then I tell you what happens. private void AddNewUserToDataBase(User user) { string commandText = "INSERT INTO User (firstName,lastName,adress,town,favoriteCurrency) " + "VALUES (@firstName,@lastName,@adress,@town,@favoriteCurrency)"; using (SqlConnection connection = new SqlConnection(global::Laboratoire1.Properties.Settings.Default.Database1ConnectionString)) { using (SqlCommand command = new SqlCommand()) { command.Connection = connection; command.CommandText = commandText;

ExecuteNonQuery() for Insert

匆匆过客 提交于 2019-12-02 04:11:18
Can you please tell me what's wrong with this code? Do I need to use DataAdapter to insert into a table? I know the connectionString is ok, because I tested it on the Server Explorer. Dim mydao As New Connection Dim connectionString As String = mydao.GetConnectionString() Dim connection As New SqlConnection(connectionString) Dim cmd As New SqlCommand Public Function add(ByVal area As String, ByVal user As String) As Integer cmd.CommandText = "INSERT into Area (Area, user) VALUES ('" + area + "','" + user + "')" Try connection.Open() Dim cant As Integer = cmd.ExecuteNonQuery()'it throws

OracleCommand command, ExecuteNonQuery issue

℡╲_俬逩灬. 提交于 2019-12-01 19:00:48
I have to clear certain tables in the oracle database however when I'm having issues with running the following code public static void ClearDataTables(IList<string> tableNames) { string connectionString = "CONNECTIONSTRING"; using (OracleConnection connection = new OracleConnection()) { connection.ConnectionString = connectionString; connection.Open(); foreach (string table in tableNames) { OracleCommand command = connection.CreateCommand(); string sql = String.Format("DELETE FROM TOA_REPORTING.{0}", table); command.CommandText = sql; command.ExecuteNonQuery(); } connection.Close(); } } I am

c#: ExecuteNonQuery() returns -1

亡梦爱人 提交于 2019-12-01 15:27:45
Ive used this method before to return the amount of rows changed. I am it to run an insert method, the insert runs fine in the stored procedure, but the return value from the ExecuteNonQuery always returns -1. Here is my C# code: int ret = 0; using (SqlConnection conn = new SqlConnection(this.ConnectionString)) { using (SqlCommand cmd = new SqlCommand(QueryName, conn)) { conn.Open(); if (Params != null) cmd.Parameters.AddRange(Params); cmd.CommandType = CommandType.StoredProcedure; ret = cmd.ExecuteNonQuery(); conn.Close(); } } return ret; Why do I get -1 instead of the actual number of rows

How can I insert more than 8000 characters in a VARCHAR(MAX) column with ExecuteNonQuery?

依然范特西╮ 提交于 2019-12-01 10:51:38
I am trying to insert > 8000 characters (submit from a web page) via ExecuteNonQuery (and DatabaseFactory.CreateDatabase() from MS Practices Enterprise Library). The stored procedure defines the parameter as VARCHAR(MAX) . The column is VARCHAR(MAX) . In theory, 2GB of data should be able to be passed. What can I do to pass data > 8000? I set a breakpoint and the string.Length is indeed > 8K. public static void UpdateTerms(string terms) { Database db = DatabaseFactory.CreateDatabase(); db.ExecuteNonQuery("uspUpdateTerms", terms); } Stored procedure: ALTER PROCEDURE [dbo].[uspUpdateTerms]

Update statement with Entity Framework

主宰稳场 提交于 2019-12-01 04:05:17
Simple question, is it possible to achieve this query this with Entity Framework when updating one entity? update test set value = value + 1 where id = 10 Not really under this form no. You will have to select all entities that match your criteria, foreach over them and update them. If you are looking for something that will do it right in the DB because your set could be huge, you will have to use SQL directly. (I don't remember if EF has a way to execute UPDATE queries directly the way Linq To SQL does). Use the Batch Update feature of the Entity Framework Extended Library , like this:

Error while using ExecuteNonQuery c#

对着背影说爱祢 提交于 2019-11-29 08:42:47
I have a problem while using ExecuteNonQuery . This is my code : var connString = @"Data Source=serwer01;Initial Catalog=PolsatCyfrowy;Integrated Security=True"; FileInfo file = new FileInfo("C:\\Users\\azbudniewek\\source\\repos\\UM2 V2\\UM2 V2\\scripts.sql"); string script = file.OpenText().ReadToEnd(); SqlConnection conn = new SqlConnection(connString); Server server = new Server(new ServerConnection(conn)); server.ConnectionContext.ExecuteNonQuery(script); And this is the error: System.IO.FileNotFoundException: The file or set 'Microsoft.SqlServer.BatchParser, version = 14.100.0.0, Culture

How to pass variable into SqlCommand statement and insert into database table

£可爱£侵袭症+ 提交于 2019-11-28 14:13:56
I'm writing a small program in C# that uses SQL to store values into a database at runtime based on input by the user. The only problem is I can't figure out the correct Sql syntax to pass variables into my database. private void button1_Click(object sender, EventArgs e) { int num = 2; using (SqlCeConnection c = new SqlCeConnection( Properties.Settings.Default.rentalDataConnectionString)) { c.Open(); string insertString = @"insert into Buildings(name, street, city, state, zip, numUnits) values('name', 'street', 'city', 'state', @num, 332323)"; SqlCeCommand cmd = new SqlCeCommand(insertString,