简体   繁体   中英

Disconnected ADO.Net mode, load from Datable to Sql Table

I have two Databases Movies(id, name) and SessionsCinema(id, SessionTime, movie, hall, price). id - is auto increment row.

I work in ado.net Disconnected mode with ms sql database. I want to save settings to sql table. Changes to table "Movies" is ok, but with table "SessionsCinema" there are some errors.

//connection start
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
builder.DataSource = "MYPC\SQLEXPRESS";
builder.InitialCatalog = "Cinema";
builder.IntegratedSecurity = true;
SqlConnection conn = new SqlConnection(builder.ConnectionString);
//connection end


//Movies start
SqlCommand inscmd = new SqlCommand();
inscmd.CommandText = "Insert into Movies (name) values(@name); select id = @@IDENTITY from Movies";
inscmd.Connection = conn;
inscmd.Parameters.Add("@name", SqlDbType.NVarChar, 250, "name");

SqlDataAdapter adapter = new SqlDataAdapter(inscmd);
adapter.InsertCommand = inscmd;
adapter.Update(Movies);
//Movies end

//SessionsCinema start
inscmd = new SqlCommand();
inscmd.CommandText = "Insert into SessionsCinema (SessionTime, movie, hall, price) values(@SessionTime, @movie, @hall, @price); select id = @@IDENTITY from SessionsCinema";
inscmd.Connection = conn;
inscmd.Parameters.Add("@SessionTime,@movie,@hall,@price", SqlDbType.NVarChar, 250, "SessionTime,movie,hall,price");

adapter = new SqlDataAdapter(inscmd);
adapter.InsertCommand = inscmd;
adapter.Update(SessionsCinema);
//SessionsCinema end

You must look at closer to SqlParameterCollection.Add signature.

//SessionsCinema start
            inscmd = new SqlCommand();
            inscmd.CommandText = "Insert into SessionsCinema (SessionTime, movie, hall, price) values(@SessionTime, @movie, @hall, @price); select id = @@IDENTITY from SessionsCinema";
            inscmd.Connection = conn;
            inscmd.Parameters.Add("@SessionTime, SqlDbType.NVarChar, 250).Value = SessionTime;
            inscmd.Parameters.Add("@movie", SqlDbType.NVarChar, 250).Value = movie;
            inscmd.Parameters.Add("@hall", SqlDbType.NVarChar, 250).Value = hall;
            inscmd.Parameters.Add("@price", SqlDbType.NVarChar, 250).Value = price;
            adapter = new SqlDataAdapter(inscmd);
            adapter.InsertCommand = inscmd;
            adapter.Update(SessionsCinema);
//SessionsCinema end

This line is wrong:

inscmd.Parameters.Add("@SessionTime,@movie,@hall,@price", SqlDbType.NVarChar, 250, "SessionTime,movie,hall,price");

You need a separate Parameters.Add() call for each of the parameters in that list. It should look something like this:

inscmd.Parameters.Add("@SessionTime", SqlDbType.NVarChar, 250).Value = SessionTime;
inscmd.Parameters.Add("@movie", SqlDbType.Int).Value = movie;
inscmd.Parameters.Add("@hall", SqlDbType.Int).Value = hall;
inscmd.Parameters.Add("@price", SqlDbType.Money).Value = price;

Also, check your parameter from your first command. It looks like you made the same mistake there.

Your first command works because it has only one parameter and you define one .Add method for it. That's ok.

But your second command has 4 parameter and you can't add 4 parameter in just one .Add method.

You need to add them seperatly with SqlParameterCollection.AddWithValue like;

inscmd.Parameters.AddWithValue("@SessionTime, SessionTime);
inscmd.Parameters.AddWithValue("@movie", movie);
inscmd.Parameters.AddWithValue("@hall", hall);
inscmd.Parameters.AddWithValue("@price", price);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM