C# Insert是一个变量,但其使用方式类似于从主类调用class1的方法

C# Insert是一个变量,但其使用方式类似于从主类调用class1的方法,c#,winforms,ms-access,C#,Winforms,Ms Access,我不知道我做错了什么。我试图调用主类中的class1.cs,即SQL命令参数,但我得到了错误。我是从我以前的线程工作 如果有人能帮助我,我将不胜感激,提前谢谢 C类 public static OleDbConnection GetConnection() { var myCon = new OleDbConnection(); myCon.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Dat

我不知道我做错了什么。我试图调用主类中的class1.cs,即SQL命令参数,但我得到了错误。我是从我以前的线程工作 如果有人能帮助我,我将不胜感激,提前谢谢

C类

 public static OleDbConnection GetConnection()
    {
        var myCon = new OleDbConnection();
        myCon.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data  
        Source=C:\...Database1.mdb";
        return myCon;
    }

    public static void Insert(string id, string agegroup, string gender, string crimoff, string photoa, string cv)
    {

        var con = GetConnection();
        OleDbCommand cmd = new OleDbCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "INSERT INTO Table1 (ID, AgeGroup, Gender, CriminalOffence, photo, CV )";
        cmd.Parameters.AddWithValue("@ID", id);
        cmd.Parameters.AddWithValue("@AgeGroup", agegroup);
        cmd.Parameters.AddWithValue("@Gender", gender);
        cmd.Parameters.AddWithValue("@CriminalOffence", crimoff);
        cmd.Parameters.AddWithValue("@photo", photoa);
        cmd.Parameters.AddWithValue("@CV", cv);
        cmd.Connection = con;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
我得到错误的主窗体类

 private void btnInsert_Click(object sender, EventArgs e)
    {

        Class1 Insert = new Class1();
        Insert(textBox1.Text, comboBox1.Text, comboBox2.Text, rBYes.Text, rBNo.Text, // error pointing at Insert line
        pictureBox1.Image, richTextBox1.Text);

        if (pictureBox1.Image != null)
        {
            //using MemoryStream:
            ms = new MemoryStream();
            pictureBox1.Image.Save(ms, ImageFormat.Jpeg);
            byte[] photo_aray = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(photo_aray, 0, photo_aray.Length);
            cmd.Parameters.AddWithValue("@photo", photo_aray);
        }

您不需要创建对象来调用静态方法。如果该方法位于同一命名空间上,则可以像下面那样调用direct方法

Insert(textBox1.Text, comboBox1.Text, comboBox2.Text, rBYes.Text, rBNo.Text, 
        pictureBox1.Image, richTextBox1.Text);
但将创建的对象的名称更改为
Insert

Class1 Insert = new Class1(); // remove this line
如果您的方法
Insert
写在
Class1
中,那么您可以如下调用它

Class1.Insert(textBox1.Text, comboBox1.Text, comboBox2.Text, rBYes.Text, rBNo.Text, 
        pictureBox1.Image, richTextBox1.Text);
  • 您的Insert方法是静态方法。我觉得你应该把类名放在前面
  • Insert方法有6个参数,但您尝试插入7个参数
  • 您的参数并非全部为string类型。一种是图像类型

    • 这是因为您在尝试调用Insert函数之前创建了一个名为Insert的变量。编译器正在查看该变量并试图将其用作函数-在本例中无法完成此操作


      实际上,您不需要创建Class1的实例,正如其他海报所说,因为Insert函数是静态的。

      Try
      Class1.Insert(rest
      …这是一个静态方法。@MartinMulder-Damith解决方案有效,但pictureBox1.Image出现错误…参数5:无法从'System.Drawing.Image'转换为'string'?谢谢Damith,解决了问题。pictureBox1.Image出现错误…参数5:无法从'System.Drawing.Image'转换为'string'。我是否要再创建一个她的帖子?是的,创建新问题。如果它解决了你的问题,请接受答案。谢谢Damith。你的解决方案已经奏效了。