Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I need to save data inside an sqlserver

What I have tried:

C#
using (SqlConnection conn = new SqlConnection(@"Data source = ******* ; Database=********* ; User Id=sa ; Password=********"))
                    {

                        SqlCommand command = new SqlCommand("IF OBJECT_ID('UXFaturas', 'U') IS NULL CREATE TABLE UXFaturas(NumFaturas char(250) NOT NULL, Cont1 char(250) NOT NULL, Cont2 char(250) NOT NULL)" +
                            "INSERT INTO UXFaturas (NumFaturas, Cont, Cont2)", conn);
                        
                        
                        command.Parameters.AddWithValue("@NumFaturas", numfatura);
                        command.Parameters.AddWithValue("@Cont1", cont);
                        command.Parameters.AddWithValue("@Cont2", cont2);
                    conn.Open();
                    command.CommandType = CommandType.Text;
                        command.ExecuteNonQuery();
                    conn.Close();
                }


I've tried this but is not inserting. There are no errors but the table stills empty.
Posted
Updated 21-Aug-17 7:55am
v5

Look at your code carefully. You are calling ExecuteNonQuery before you even add your parameters. You also need to change your sql statement to use parameters.
SqlCommand insertCommand = new SqlCommand("INSERT INTO UXFaturas(NumFaturas, Cont1, Cont2) VALUES (@numfatura, @cont, @cont2)", conn);
Add the @ symbol in the VALUES clause.
 
Share this answer
 
Comments
Member 13356973 17-Aug-17 8:19am    
I'm calling ExecuteNonQuery before because if I do i after i'll get an error
ZurdoDev 17-Aug-17 8:20am    
Then fix the error. When you call ExecuteNonQuery is when it calls into sql so if you haven't added in your parameters it does no good.
Member 13356973 17-Aug-17 8:20am    
This is de error I get:

System.Data.SqlClient.SqlException: 'Could not find stored procedure 'IF OBJECT_ID('UXFaturas', 'U') IS NULL CREATE TABLE UXFaturas(NumFaturas char(250), Cont1 char(250), Cont2 char(250))'.'
ZurdoDev 17-Aug-17 8:22am    
Think about what the error is telling you and then look at your code. It think you are trying to call a stored procedure because you told it you were going to with this line:
command.CommandType = System.Data.CommandType.StoredProcedure;

Change the commandType to Text.
Member 13356973 17-Aug-17 8:25am    
Now I got no error, it should be working now right?
You are calling command.ExecuteNonQuery(); before you have set any of the parameters in your command object. You are also specifying to use a stored procedure when you do not need one.
 
Share this answer
 
v2
Comments
Member 13356973 17-Aug-17 8:19am    
I'm calling ExecuteNonQuery before because if I do i after i'll get an error
Richard MacCutchan 17-Aug-17 8:53am    
As ridiculous ideas go that has to be near the top of the list. I suggest you use Google to find some tutorials on SQL (and maybe even programming in general) before trying this again.
I'd suggest splitting out the table creation part to a separate command. If the table doesn't exist, SQL might struggle to prepare the query to insert data into it.

Your INSERT command is currently missing a VALUES clause, so you should be getting an exception telling you the syntax is not correct.
C#
using (SqlConnection conn = new SqlConnection(@"Data source = 2c4138928627\Sage ; Database=ARMINDOData ; User Id=sa ; Password=sage2008+"))
{
    conn.Open();
    
    using (SqlCommand command = new SqlCommand("IF OBJECT_ID('UXFaturas', 'U') IS NULL CREATE TABLE UXFaturas(NumFaturas char(250) NOT NULL, Cont1 char(250) NOT NULL, Cont2 char(250) NOT NULL)", conn))
    {
        command.CommandType = CommandType.Text;
        command.ExecuteNonQuery();
    }
    
    using (SqlCommand command = new SqlCommand("INSERT INTO UXFaturas (NumFaturas, Cont, Cont2) VALUES (@NumFaturas, @Cont, @Cont2)", conn))
    {
        command.CommandType = CommandType.Text;
        command.Parameters.AddWithValue("@NumFaturas", numfatura);
        command.Parameters.AddWithValue("@Cont1", cont);
        command.Parameters.AddWithValue("@Cont2", cont2);
        command.ExecuteNonQuery();
    }
}

INSERT (Transact-SQL) | Microsoft Docs[^]

NB: Ideally, you should avoid connecting as sa. That user has unrestricted access to the entire server, which your application doesn't need. Instead, you should use an account which has only the permissions required by your application.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900