Insert into the database "MDF" does not work
Asked By: Ronald Araújo
Originally Asked On: 2014-01-05 19:52:01
Asked Via: stackoverflow
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|BaseDatabase.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
He received 2 answers
eventually accepting:
CRAFTY DBA’s answer to
Insert into the database "MDF" does not work
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.
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
Image may be NSFW.
Clik here to view.
If the selected answer did not help you out, the other answers might!
All Answers For: Insert into the database "MDF" does not work
mopicus’s answer to
Insert into the database "MDF" does not work
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(); } }
CRAFTY DBA’s answer to
Insert into the database "MDF" does not work
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.
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
Image may be NSFW.
Clik here to view.
Of course, you should really check out the original question.
The post Insert into the database "MDF" does not work [ANSWERED] appeared first on Tech ABC to XYZ.