|
-
Mar 4th, 2007, 09:18 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Insert problem
HI,
I have a problem during inserting my new record but if I change to select statement. I can display the datareader value. Do you have any idea?
Code:
string contring;
private void Form1_Load(object sender, EventArgs e)
{
contring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Book1.xls;Extended Properties=""Excel 8.0;HDR=YES;IMEX=1""";
}
private void button2_Click(object sender, EventArgs e)
{
DbProviderFactory dpF = DbProviderFactories.GetFactory("System.Data.OleDb");
{
using (DbConnection dbCon= dpF.CreateConnection())
{
dbCon.ConnectionString = contring;
DbCommand dbCom = dbCon.CreateCommand();
//dbCom.CommandText = "select * from [sheet1$]";
dbCom.CommandText = "insert into[sheet1$](first,second)values(\"unse\",\"unse\")";
dbCon.Open();
DbDataReader dreader = dbCom.ExecuteReader();
// while (dreader.Read())
// {
// MessageBox.Show(dreader["first"].ToString());
// }
dbCon.Close();
}
Thanks,
Popskie
Last edited by popskie; Mar 22nd, 2007 at 02:21 AM.
-
Mar 4th, 2007, 10:31 PM
#2
Re: Insert problem
Firstly, I would question why you're using a DataReader to insert data. While a DataReader can be used to insert data it is usually only done when the insert is followed by a query. As the name suggests, a DataReader is for reading data. If you're not reading any data then don't use one. To execute a non-query SQL statement you call the aptly named ExecuteNonQuery method rather than ExecuteReader.
As for your issue, text values in SQL statements must be enclosed in single quotes. If you want to insert the value "unse" then your code needs to look like this:
c# Code:
dbCom.CommandText = "insert into [sheet1$] (first, second) values ('\"unse\"', '\"unse\"')";
-
Mar 4th, 2007, 11:37 PM
#3
Thread Starter
Fanatic Member
Re: Insert problem
Just ingnore executereader cause it came from a select statement. JM i tried your suggestion but I encounter same error. Another question do I need to enclosed a backslash in every value cause in sql is not?
-
Mar 5th, 2007, 07:31 PM
#4
Re: Insert problem
What value are you actually trying to insert? Does it include the double quotes or not? If so then you must either escape them as I have above. If the values don't include the double quotes then they shouldn't be there. As I said, SQL encloses text literals in single quotes:
c# Code:
dbCom.CommandText = "insert into [sheet1$] (first, second) values ('unse', 'unse')";
Also, do you have columns named "first" and "second"?
-
Mar 5th, 2007, 08:38 PM
#5
Thread Starter
Fanatic Member
Re: Insert problem
 Originally Posted by jmcilhinney
What value are you actually trying to insert? Does it include the double quotes or not? If so then you must either escape them as I have above. If the values don't include the double quotes then they shouldn't be there. As I said, SQL encloses text literals in single quotes:
c# Code:
dbCom.CommandText = "insert into [sheet1$] (first, second) values ('unse', 'unse')";
Also, do you have columns named "first" and "second"?
JM thanks for the reply again. I also try that one before it work if im using MS SQL but in excel it is not and yes I have columns first and second. I can display all the value of this columns using select But if I try to add another record, error raise. I have so many experience in ado.nt programming like access, ms sql, mysql, foxpro. Only in excel I encounter this error. I think there is a property I missing in the connection string.
-
Mar 6th, 2007, 01:10 AM
#6
Re: Insert problem
Dunno. Never inserted into an Excel sheet using ADO.NET.
-
Mar 6th, 2007, 10:27 AM
#7
Hyperactive Member
Re: Insert problem
I didn't know you could use ADO.NET to manipulate Excel worksheets. What else can you use ADO.NET to manipulate?
Currently Using: VS 2005 Professional
-
Mar 6th, 2007, 05:34 PM
#8
Re: Insert problem
 Originally Posted by tacoman667
I didn't know you could use ADO.NET to manipulate Excel worksheets. What else can you use ADO.NET to manipulate?
Anything you like. If a provider doesn't exist you can write your own. See www.connectionstrings.com for connection string formats for common providers.
-
Mar 6th, 2007, 08:41 PM
#9
Thread Starter
Fanatic Member
Re: Insert problem
Ok JM I try to research more. I mark this as solved.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|