executenonquery

Retrieving error codes from SQLite when using ExecuteNonQuery()

谁都会走 提交于 2019-11-28 13:03:48
In my C# project, I'm using System.Data.SQLite.dll downloaded from CodeProject . My problem is as per the title - how to get the error codes after calling SqliteCommand.ExecuteNonQuery() function? Error codes such as SQLITE_CONSTRAINT, SQLITE_BUSY, SQLITE_LOCKED as shown here . use the Exception.StackTrace or the SQLiteException.ErrorCode try { } catch(SQLiteException ex) { string code = ex.ErrorCode; } I'm going to add to this to help others, if you're developing in .NET. Use the SQLiteErrorCode enumeration to test the result, cast the ErrorCode: try { } catch(SQLiteException ex) {

Inserting values into a SQL Server database using ado.net via C#

梦想的初衷 提交于 2019-11-28 06:28:32
I have created a simple program to insert values into the table [regist] , but I keep getting the error Incorrect syntax near ')' on cmd.ExecuteNonQuery(); : private void button1_Click(object sender, EventArgs e) { SqlConnection cn = new SqlConnection("Data Source=DELL-PC;initial catalog=AdventureWorks2008R2 ; User ID=sa;Password=sqlpass;Integrated Security=SSPI;"); SqlCommand cmd = new SqlCommand("INSERT INTO dbo.regist (" + " FirstName, Lastname, Username, Password, Age, Gender,Contact, " + ") VALUES (" + " @textBox1.Text, @textBox2.Text, @textBox3.Text, @textBox4.Text, @comboBox1.Text,

Error while using ExecuteNonQuery c#

 ̄綄美尐妖づ 提交于 2019-11-28 02:05:43
问题 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

Get affected rows on ExecuteNonQuery

梦想的初衷 提交于 2019-11-27 09:00:32
I am currently working on a C# project and I am running an insert query which also does a select at the same time, e.g.: INSERT INTO table (SELECT * FROM table WHERE column=date) Is there a way I can see how many rows were inserted during this query? ExecuteNonQuery - returns the number of rows affected. SqlCommand comm; // other codes int numberOfRecords = comm.ExecuteNonQuery(); If you run the SQL from your question in a SqlCommand and check the return value of ExecuteNonQuery it should tell you how many records were affected. From the documentation : Return Value Type: System.Int32 The

Retrieving error codes from SQLite when using ExecuteNonQuery()

爱⌒轻易说出口 提交于 2019-11-27 07:32:18
问题 In my C# project, I'm using System.Data.SQLite.dll downloaded from CodeProject. My problem is as per the title - how to get the error codes after calling SqliteCommand.ExecuteNonQuery() function? Error codes such as SQLITE_CONSTRAINT, SQLITE_BUSY, SQLITE_LOCKED as shown here. 回答1: use the Exception.StackTrace or the SQLiteException.ErrorCode try { } catch(SQLiteException ex) { string code = ex.ErrorCode; } 回答2: I'm going to add to this to help others, if you're developing in .NET. Use the

ExecuteNonQuery returning -1 when using sql COUNT despite the query string

人走茶凉 提交于 2019-11-27 05:30:41
For some reason, ExecuteNonQuery() in C# returns -1 , though when I run a query separately, the value returns the actual value needed. For Example: try { var connString ="Data Source=ServerName;InitialCatalog=DatabaseName;Integrated Security=true;" SqlConnection conn = new SqlConnection(connString); SqlCommand someCmd = new SqlCommand("SELECT COUNT(*) FROM SomeTable"); someCmd.Connection = conn; conn.Open(); var theCount = cmd.ExecuteNonQuery(); conn.Close(); } catch(Exception ex) { Console.WriteLine(ex.Message); } When the command is executed it returns -1 . Though if run the query separately

ExecuteScalar vs ExecuteNonQuery when returning an identity value

蹲街弑〆低调 提交于 2019-11-27 04:05:09
Trying to figure out if it's best to use ExecuteScalar or ExecuteNonQuery if I want to return the identity column of a newly inserted row. I have read this question and I understand the differences there, but when looking over some code I wrote a few weeks ago (whilst heavily borrowing from this site) I found that in my inserts I was using ExecuteScalar , like so: public static int SaveTest(Test newTest) { var conn = DbConnect.Connection(); const string sqlString = "INSERT INTO dbo.Tests ( Tester , Premise ) " + " VALUES ( @tester , @premise ) " + "SET @newId = SCOPE_IDENTITY(); "; using (conn

Get affected rows on ExecuteNonQuery

人盡茶涼 提交于 2019-11-26 14:26:07
问题 I am currently working on a C# project and I am running an insert query which also does a select at the same time, e.g.: INSERT INTO table (SELECT * FROM table WHERE column=date) Is there a way I can see how many rows were inserted during this query? 回答1: ExecuteNonQuery - returns the number of rows affected. SqlCommand comm; // other codes int numberOfRecords = comm.ExecuteNonQuery(); 回答2: If you run the SQL from your question in a SqlCommand and check the return value of ExecuteNonQuery it

ExecuteNonQuery returning -1 when using sql COUNT despite the query string

霸气de小男生 提交于 2019-11-26 11:36:46
问题 For some reason, ExecuteNonQuery() in C# returns -1 , though when I run a query separately, the value returns the actual value needed. For Example: try { var connString =\"Data Source=ServerName;InitialCatalog=DatabaseName;Integrated Security=true;\" SqlConnection conn = new SqlConnection(connString); SqlCommand someCmd = new SqlCommand(\"SELECT COUNT(*) FROM SomeTable\"); someCmd.Connection = conn; conn.Open(); var theCount = cmd.ExecuteNonQuery(); conn.Close(); } catch(Exception ex) {

ExecuteScalar vs ExecuteNonQuery when returning an identity value

。_饼干妹妹 提交于 2019-11-26 11:02:10
问题 Trying to figure out if it\'s best to use ExecuteScalar or ExecuteNonQuery if I want to return the identity column of a newly inserted row. I have read this question and I understand the differences there, but when looking over some code I wrote a few weeks ago (whilst heavily borrowing from this site) I found that in my inserts I was using ExecuteScalar , like so: public static int SaveTest(Test newTest) { var conn = DbConnect.Connection(); const string sqlString = \"INSERT INTO dbo.Tests (