Insert是一个变量,但其用法类似于方法-从Main Class调用class1 [英] Insert is a variable but is used like a method - calling class1 from Main Class

查看:198
本文介绍了Insert是一个变量,但其用法类似于方法-从Main Class调用class1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我在做什么错.我正在尝试在Main Class(SQL命令参数)中调用class1.cs,但出现错误.我正在以前的线程此处 如果有人可以在这里帮助我,我将不胜感激.

I don't know what I am doing wrong here. I am trying to call class1.cs in Main Class, SQL command parameter, but I getting the error. I am working from my previous thread here I would be grateful if anyone can help me out here, thanks in advance..

class.cs

 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();
    }

我遇到错误的主窗体类...

Main Form class where I am getting error...

 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);
        }

推荐答案

您无需创建对象即可调用静态方法.如果该方法在同一名称空间上,则可以调用如下所示的直接方法.

you don't need to create object to call static method. if the method is on same namespace you can call direct method like below.

Insert(textBox1.Text, comboBox1.Text, comboBox2.Text, rBYes.Text, rBNo.Text, 
        pictureBox1.Image, richTextBox1.Text);

但是将您创建的对象的名称更改为Insert

But change the name of the object you created as Insert

Class1 Insert = new Class1(); // remove this line

如果您的方法Insert写在Class1内,则可以按以下方式调用

If your method Insert written inside Class1, then you can call it as below

Class1.Insert(textBox1.Text, comboBox1.Text, comboBox2.Text, rBYes.Text, rBNo.Text, 
        pictureBox1.Image, richTextBox1.Text);

这篇关于Insert是一个变量,但其用法类似于方法-从Main Class调用class1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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