Frage

Honestly I've tried everything and in many ways, but I can not give a simple INSERT in the database. Please could someone tell me what is wrong with this code?

using (SqlConnection con = new SqlConnection(AcessoBD.ConnectionString))
{
    string queryUsuario = @"INSERT INTO si_usuario (senha, login) VALUES ('aaa', 'aaa');";

    using (SqlCommand cmd = new SqlCommand(queryUsuario, con))
    {
       con.Open();
       cmd.BeginExecuteNonQuery();

    }
}

Making it clear that I've tried with: cmd.ExecuteNonQuery(); But it did not work!

The funny thing is that I can make the bench query above using the same structure, only even changing the query. Is it a permission problem? But no error occurs!

public class AcessoBD
    {
        static public String ConnectionString
        {
            get
            {    
                return ConfigurationManager.ConnectionStrings["Conexao"].ConnectionString;
            }
        }
    }

Table Structure

CREATE TABLE [dbo].[si_usuario] (
    [id]    INT           IDENTITY (1, 1) NOT NULL,
    [senha] VARCHAR (100) NOT NULL,
    [login] VARCHAR (100) NOT NULL,
    CONSTRAINT [PK_si_usuario] PRIMARY KEY CLUSTERED ([id] ASC)
);

App.config

<add name="Conexao" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Base\Database.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
War es hilfreich?

Lösung

It must be a permission issue. Your code works fine in the SQL Server 2012 developer version. See quick test below.

I can only assume you are using SQL Server express that comes with Visual Studio and did not give the trusted user INSERT, UPDATE, DELETE permissions to the table.

Download the SQL Server Management Studio Express and start debugging your issue!

http://msdn.microsoft.com/en-us/evalcenter/hh230763

Take a look at the windows application log or SQL Server Error log to see if you are ignoring errors in your C# program. Trace the connection to the database using the profiler to make sure you are passing the INSERT.

Take a look at the security under logins / user mapping. Does the user map to the database with default schema dbo? What permission did you give them.

If they only have public, add db_datareader and db_datawriter so the can read/write to all tables.

Otherwise, GRANT them rights at the table level. But you will to do this for each table, more maintenance.

Take a look at my blog article on using SSMS to create logins/users.

http://craftydba.com/?p=4369

Good luck

-- Code works fine!
use tempdb
go

CREATE TABLE [dbo].[si_usuario] (
    [id]    INT           IDENTITY (1, 1) NOT NULL,
    [senha] VARCHAR (100) NOT NULL,
    [login] VARCHAR (100) NOT NULL,
    CONSTRAINT [PK_si_usuario] PRIMARY KEY CLUSTERED ([id] ASC)
);
go

INSERT INTO si_usuario (senha, login) VALUES ('aaa', 'aaa');
go

select * from si_usuario
go

enter image description here

Andere Tipps

Try changing to this:

using (SqlConnection con = new SqlConnection(AcessoBD.ConnectionString))
{
    con.Open();
    using (SqlCommand cmd = con.CreateCommand())
    {
        cmd.CommandText = @"INSERT INTO si_usuario (senha, login) VALUES ('aaa', 'aaa');";
        cmd.ExecuteNonQuery();

    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top