插入数据库时​​出现问题 [英] problem With insert into DB

查看:91
本文介绍了插入数据库时​​出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序成功地从数据库读取了变量

但是我在向其中插入数据时遇到了问题
我不知道为什么?
我在更多程序中使用了此代码,效果很好

C#类代码

  class 手势
    {
        //  SQL数据库连接
        SqlConnection con1 =  SqlConnection( @" );

         公共 无效 Addgesture( int  i,字符串蓝色, string 红色, string 绿色, 字符串黄色,字符串洋红色,字符串控件)
        {
            SqlCommand mycom2 =  SqlCommand();
            mycom2.Connection = con1;
            mycom2.CommandType = CommandType.StoredProcedure;
            mycom2.CommandText = " ;
            mycom2.Parameters.AddWithValue(" ,i);
            mycom2.Parameters.AddWithValue(" ,蓝色);
            mycom2.Parameters.AddWithValue(" ,红色);
            mycom2.Parameters.AddWithValue(" ,绿色);
            mycom2.Parameters.AddWithValue(" ,黄色);
            mycom2.Parameters.AddWithValue(" ,洋红色);
            mycom2.Parameters.AddWithValue(" ,控件);
            con1.Open();
            mycom2.ExecuteNonQuery();
            con1.Close();

        }
} 


C#程式码

  int  i =  77 ;
字符串 control = comboBox1.Items.ToString();
手势newg =  Gesture();
newg.Addgesture(i,FBlue2,FRed2,FGreen2,FYellow2,FMagenta2,控件);



StoredProcedure SQL代码

  ALTER  过程 AddGesture

     @ int   int  @ blue   varchar ( 50 ), @ red   varchar ( 50 ), @ green   varchar ( 50 ), @ yellow   50 ), varchar ( @ Control   50 )

 AS 


    插入 插入手势( @ int  @ blue  @ red  @绿色 @黄色 @洋红色 @ Control )

    返回 




>>>>>>>>>>>>><>< pre lang ="c#">



我在
遇到问题 mycom2.ExecuteNonQuery();

错误是
无效的对象名称手势".

[edit]已添加代码块-OriginalGriff [/edit]

解决方案

您抱怨所连接的数据库没有名为"gesture"的表. br/>
检查您已连接到正确的数据库,并且您的表名称正确.

顺便说一句,命名插入的字段也是一个好主意:这样,对数据库的更改不会弄乱您的插入内容和数据库.


检查您的数据库Database 中的表名Gesture 是否为exists .

无效的对象名称手势".
表名不存在时会出现此错误.

请检查一下.

谢谢
灰烬


My program Reads variables from DB Successfully

But i have a problem in inserting data into it
i don''t know why ?
i use this code in more programs and it works good

C# Class Code

class Gesture
    {
        //Sql DB Connection
        SqlConnection con1 = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Amr Khalid\Documents\HandGestures.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

         public void Addgesture(int i , string blue, string red, string green, string yellow, string magenta , string control)
        {
            SqlCommand mycom2 = new SqlCommand();
            mycom2.Connection = con1;
            mycom2.CommandType = CommandType.StoredProcedure;
            mycom2.CommandText = "AddGesture";
            mycom2.Parameters.AddWithValue("@int", i);
            mycom2.Parameters.AddWithValue("@blue", blue);
            mycom2.Parameters.AddWithValue("@red", red);
            mycom2.Parameters.AddWithValue("@green", green);
            mycom2.Parameters.AddWithValue("@yellow", yellow);
            mycom2.Parameters.AddWithValue("@magenta", magenta);
            mycom2.Parameters.AddWithValue("@Control", control);
            con1.Open();
            mycom2.ExecuteNonQuery();
            con1.Close();

        }
}


C# Program Code

int i = 77;
string control = comboBox1.Items.ToString();
Gesture newg = new Gesture();
newg.Addgesture(i, FBlue2, FRed2, FGreen2, FYellow2, FMagenta2, control);



StoredProcedure SQL code

ALTER PROCEDURE AddGesture

    @int int ,@blue varchar(50) , @red varchar(50) ,@green varchar(50) ,@yellow varchar(50) , @magenta varchar(50) , @Control varchar(50)

AS


    Insert into Gesture values (@int ,@blue,@red,@green,@yellow,@magenta,@Control)

    RETURN




>>>>>>>>>>>>>>>>>



i have the problem in
mycom2.ExecuteNonQuery();

The error is
Invalid object name ''Gesture''.

[edit]Code block added - OriginalGriff[/edit]

解决方案

It is complaining that the database your are connected to does not have a table called "gesture".

Check you are connected to the correct database, and that your tables name is correct.

BTW it is also a very good idea to name the fields that you are inserting into: that way changes to your DB won''t mess up your inserts and thus your database.


Check your table name Gesture in your Database whether it exists or not.

Invalid object name ''Gesture''.
This error comes when the tablename does not exist.

Please check it out.

Thanks
Ashish


这篇关于插入数据库时​​出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆