|
And what have you tried? Where are you stuck?
Never underestimate the power of stupid things in large numbers
--- Serious Sam
|
|
|
|
|
|
hi ,
i dont english good .i will import data from excel to c# win application and transfer data to sql. but i can't .i will import data from excel(some columnsnot all) to grid and insert bulk to sql .i am low level .please help .
best regards.
|
|
|
|
|
|
very thanks for your help i will study your link
best regards
|
|
|
|
|
i have all result that i want in access database and i will to display to user in their real values in datagridview that i will generate.. any one can give me a example to code that i must use.
|
|
|
|
|
|
Hello,
I have imported a CSV file into SQL, the wizard has added this as a new table.
I am trying to run a SQL Statement to insert the data from the imported table into the one I need to use in the new data table.
However I am getting the following error.
Collapse | Copy Code
Msg 2627, Level 14, State 1, Line 4
Violation of PRIMARY KEY constraint 'PK_Customer'. Cannot insert duplicate key in object 'dbo.Customer'. The duplicate key value is (105).
The statement has been terminated.
This is my statement:
Collapse | Copy Code
INSERT INTO dbo.Customer
(custID,lastName,firstName,title,address1,address2,address3,postCode,email,fax)
SELECT ACCOUNT,SURNAME,FORENAMES,TITLE,ADD1,ADD2,ADD3,POSTCODE,EMAIL,FAX
FROM dbo.[NCCUST CSV FORMAT PFS 9.1.14];
Any pointers would be great Smile | :)
Thanks
|
|
|
|
|
Well, the error says it all. There are obviously 2 customers with the value 105 in the ACCOUNT field in the 'CSV' table. You need to either remove the Primary key restraint on dbo.customer (not recommended) or remove one of the customers in the 'CSV' table with ACCOUNT 105.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
I know that you have had a great answer from Wayne, but you would better posting SQL questions next time with the correct forum Database[^]
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
Hi dudes, how can I get HotSpot and Size values of any mouse cursor, globally?
Following code works only in my own application:
Cursor.Current.HotSpot.ToString();
Tip for Windows API?
Without answer on internet or others forum...
|
|
|
|
|
|
|
Check out http://npoi.codeplex.com/[^]
You'll have to write some code to interface between your grid(s) and NPOI but in the end it works like a charm.
NPOI does support multiple sheets.
|
|
|
|
|
Member 10508431 wrote: any one have a clue ? Sounds like you'd expect a "no".
The datagridview is there to display data; it does not know about formats. If you want to save your data in Excel, you'd best search for some examples on writing data to Excel.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hello!
I was having some trouble with this error, but i added the command, and now i receive another error. Please, check out my code:
protected void btnAdd_Client(object sender, System.EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=JOHN-PC\SQLEXPRESS;Initial Catalog=subiect1;Integrated Security=True");
try
{
con.Open();
SqlCommand command = new SqlCommand("SET IDENTITY_INSERT Clienti ON");
command.ExecuteNonQuery();
SqlCommand cmd = new SqlCommand("insert into Clienti(ID, Nume, Prenume, Data_Nasterii, Adresa_mail) values(@ID, <a href="/Members/nume">@Nume</a> @Prenume, @Data_Nasterii, @Adresa_mail)", con);
cmd.Parameters.AddWithValue("@ID", txtID.Text);
cmd.Parameters.AddWithValue("@Nume", txtNume.Text);
cmd.Parameters.AddWithValue("@Prenume", txtPrenume.Text);
cmd.Parameters.AddWithValue("@Data_Nasterii", txtDataNastere.Text);
cmd.Parameters.AddWithValue("@Adresa_mail", txtAdresamail.Text);
int count = cmd.ExecuteNonQuery();
if (count == 1)
lblMsg.Text = "Clientul [" + txtID.Text + "] a fost adaugat!";
else
lblMsg.Text = "Clientul nu a putut fi adaugat!";
}
catch (Exception ex)
{
lblMsg.Text = "Error --> " + ex.Message;
}
finally
{
SqlCommand command = new SqlCommand("SET IDENTITY_INSERT Clienti OFF");
command.ExecuteNonQuery();
con.Close();
}
}
This is a function which adds new clients in my local database. As you can see, i placed the set_identity commands before and after the actual adding client code. What do i miss here?
|
|
|
|
|
Member 10513597 wrote: now i receive another error
There's a problem with my car. How do I fix it?
We can't read your mind, and we can't see your screen. If you don't tell us what the error is, we can't help you.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Well, i provided the code and i asked you to look over it, maybe you discover something wrong, which i haven't seen before. That was the main purpose of my post.
|
|
|
|
|
But if you don't tell us what error you're getting, we have no way of finding out what's wrong with your code.
If you took your car to the garage to get it fixed, would you say, "There's a problem with my car. Here's the car. Fix it.", or would you give them the details of the problem so that they'd know where to start?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Why are you constantly turning IDENTITY_INSERT on and off (it's always on by default)?? This should normally be turned on and left alone. It should be turned off only for importing existing data, for example, during a migration of the database. This should not be done as normal course for inserting a record.
|
|
|
|
|
So you're suggesting to only remove the identity_insert off from the finally block? I tried to do so, but now i get the error;
ExecuteNonQuery: Connection property has not been initialized.
Which probably refers to the part:
SqlCommand command = new SqlCommand("SET IDENTITY_INSERT Clienti ON");
command.ExecuteNonQuery();
What does this mean?
|
|
|
|
|
You are executing a Sql command without assigning a connection to it. You can either include it in the constructor on the SqlCommand or you can add it explicitly through
command.Connection = ...
|
|
|
|
|
Do i need to say:
command.Connection = new SqlConnection(@"Data Source=JOHN-PC\SQLEXPRESS;Initial Catalog=subiect1;Integrated Security=True");
or something like that?
I repeat, i am new with C#. If someone knows and is willing to answer my questions, i will be very grateful. If they don't have anything useful to say, they are asked to keep their opinions for themselves.
Thank you very much!
|
|
|
|
|
I would tend to do something like this:
using (SqlConnection connection = new SqlConnection(... put connection details in here...)
{
command.Connection = connection;
command.ExecuteNonQuery();
} The reason you would do this is that the connection to the database is closed as soon as possible after you have finished with your query.
|
|
|
|
|
Thanks, i solved the problem. It works now!
|
|
|
|