I have a problem storing my inserted data to my database. When I’m using mdf file that created within of visual studio it won’t work. When I’m using dbo file that created from SQL Server 2008 it when I tried to store my inserted data to database it worked well.
I’m using a stored procedure. There was no error occurred. help me pls.
here is the code using sqlcommand:
SqlConnection myConn = new SqlConnection(@"Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\FifthColumn.mdf; Integrated Security=True; User Instance=True");
myConn.Open();
SqlCommand mycommand = myConn.CreateCommand();
mycommand.CommandText = "InsertIncident";
mycommand.CommandType = CommandType.StoredProcedure;
mycommand.Parameters.Add("@Country", SqlDbType.NChar, 2, "Country").Value = inputCountry;
mycommand.Parameters.Add("@IncidentTypeID", SqlDbType.NChar, 2, "Country").Value = inputIncidentTypedID;
mycommand.Parameters.Add("@AgentID", SqlDbType.NChar, 2, "Country").Value = inputAgentID;
mycommand.Parameters.Add("@incidentDate", SqlDbType.SmallDateTime).Value = inputID;
mycommand.ExecuteNonQuery();
myConn.Close();
here is the code using dataadapter:
SqlConnection myConn = new SqlConnection(@"Data Source=.\SQLEXPRESS; AttachDbFilename=|DataDirectory|\FifthColumn.mdf; Integrated Security=True; User Instance=True");
SqlDataAdapter myDA = new SqlDataAdapter();
myConn.Open();
myDA.InsertCommand = myConn.CreateCommand();
myDA.InsertCommand.CommandText = "InsertIncident";
myDA.InsertCommand.CommandType = CommandType.StoredProcedure;
myDA.InsertCommand.Parameters.Add("@Country", SqlDbType.NChar, 2, "Country").Value = inputCountry;
myDA.InsertCommand.Parameters.Add("@IncidentTypeID", SqlDbType.NChar, 2, "Country").Value = inputIncidentTypedID;
myDA.InsertCommand.Parameters.Add("@AgentID", SqlDbType.NChar, 2, "Country").Value = inputAgentID;
myConn.Close();
Data adapter automatically opens and closes the connection. if you use data adapter, it is unnecessary to open or close the connection.
Second, data adapter is mostly used to fetch the block data from database into a dataset by using fill method of data adapter.
You can use insert command or update command property of data adapter to insert or update any data. They must be assigned to a command object before use.