操作数类型冲突:表类型与int不兼容

时间:2015-11-12 02:20:46

标签: c# sql-server-2008

我试图通过表

将整数值的集合传递给存储过程

请在下面找到C#代码

 int i = lbActors.Items.Count;
            int[] actors = new int[i];
            int j = 0;

            foreach (ListItem item in lbActors.Items)
            {
                if (item.Selected == true)
                {
                    actors[j] = Convert.ToInt32(item.Value);
                    j++;
                }
            }

            DataTable dt = new DataTable("movieAndActorType");
            dt.Columns.Add("actorId",typeof(Int32));
            int k=0;
            while(actors[k]!=0)
            {
                dt.Rows.Add(actors[k]);
                k++;
            }
            try
            {
                sc.CommandText = "usp_AddMovie";
                sc.Connection = sqlCon;
                sc.CommandType = CommandType.StoredProcedure;
                sc.Parameters.Add("@producerId", SqlDbType.Int).Value = Convert.ToInt32(ddlProducer.SelectedValue);
               sc.Parameters.Add("@actorIds", SqlDbType.Structured).Value = dt;
                sc.Parameters.Add("@movieName", SqlDbType.VarChar).Value = txtName.Text;
                sc.Parameters.Add("@yearOfRelease", SqlDbType.Int).Value = Convert.ToInt32(txtReleaseYear.Text);
                sc.Parameters.Add("@plot", SqlDbType.VarChar).Value = txtPlot.Text;
                sc.Parameters.Add("@image", SqlDbType.Image).Value = imgbyte;
                sqlCon.Open();
                sc.ExecuteNonQuery();
                sqlCon.Close();
            }
            catch(SqlException ex)
            {
                Console.WriteLine(" SQL error" + ex.ToString());
            }

在" sql server"我正在尝试将数据输入以下两个表

create table tbl_movie
(
movieId int primary key IDENTITY(1,1),
producerId int foreign key references tbl_producer(producerId),
movieName varchar(30) not null unique,
yearOfRelease int not null check(yearOfRelease>1900 and yearOfRelease<=datepart(yyyy,getDate())),
plot varchar(5000) not null,
poster image 
);


   create table tbl_movieAndActor                 (
movieId int foreign key references tbl_movie(movieId),
actorId int foreign key references tbl_actor(actorId),
constraint pk_movieAndActor primary key (movieId,actorId)
);

创建用户定义类型以将数据从应用程序传递到过程

     create type movieAndActorType As Table
 (
  actorId int not null
 )

和存储过程

create proc proc_AddMovie(@producerId int,@actorIds As movieAndActorType Readonly,@movieName varchar(50),@yearOfRelease int,@plot varchar(500),@image image)
 as
 begin

 insert into tbl_movie(producerId,movieName,yearOfRelease,plot,poster) values(@producerId,@movieName,@yearOfRelease,@plot,@image)

 insert into tbl_movieAndActor(movieId,actorId) select scope_identity(),actorId from @actorIds

 end

我测试了sql server中的存储过程它在那里工作正常,但是当我通过我的应用程序传递值时,它会抛出sql异常。

     "Operand type clash: table type is incompatible with int"

我在编码方面很天真,请帮我解决这个问题。

谢谢

3 个答案:

答案 0 :(得分:0)

您需要修复C#代码并确保它首先运行。你的while循环永远不会工作。这是填充表格的方法:

            DataTable dt = new DataTable("movieAndActorType");
            dt.Columns.Add("actorId", typeof(Int32));               

            for (int i = 0; i < actors.Length; i++)
            {
                var newRow = dt.NewRow();
                newRow["actorId"] = actors[i];
                dt.Rows.Add(newRow);
            }

尝试将此插入到tbl_MovieAndActor表中:         INSERT INTO tbl_movieAndActor(movieId,actorId)         SELECT movieId = @movieId,来自@actorIds的actorId

答案 1 :(得分:0)

这一切都适用于此C#代码和存储过程:

     int[] actors = { 7, 8, 9 }; // Actor IDs in database                       

        DataTable dt = new DataTable("movieAndActorType");
        dt.Columns.Add("actorId", typeof(Int32));

        for(int i = 0;i <actors.Length; i++)
        {
            var dRow = dt.NewRow();
            dRow["actorId"] = actors[i];
            dt.Rows.Add(actors[i]);                
        }
        var img = new Byte[5];

        try
        {
            using (var conn = new SqlConnection("Your connection string"))
            {
                using (var cmd = new SqlCommand())
                {
                    cmd.CommandText = "usp_AddMovie";
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@producerId", SqlDbType.Int).Value =   
                    Convert.ToInt32("99");
                    cmd.Parameters.Add(new SqlParameter { ParameterName = 
                   "@actorIds", SqlDbType = SqlDbType.Structured, Value = dt, 
                    TypeName = "dbo.movieAndActorType" });
                    cmd.Parameters.Add("@movieName", SqlDbType.VarChar).Value 
                    = "Spectre";
                    cmd.Parameters.Add("@yearOfRelease", SqlDbType.Int).Value 
                    = Convert.ToInt32("1972");
                    cmd.Parameters.Add("@plot", SqlDbType.VarChar).Value = 
                    "Some Plot";
                    cmd.Parameters.Add("@image", SqlDbType.Image).Value =                                                             
                    img;
                    conn.Open();
                    cmd.ExecuteNonQuery();
                }
            }
        }
        catch (SqlException ex)
        {
            Console.WriteLine(" SQL error" + ex.Message);
        }

答案 2 :(得分:0)

我终于找到了错误的原因,我在代码中使用了错误的过程名称。程序名称为&#34; Proc_addMovie&#34;但是我正在使用&#34; usp_AddMpvie&#34;。在浪费了很多时间之后我终于注意到了它。