ado.net代码不起作用为什么? [英] the ado.net code is not working why?

查看:89
本文介绍了ado.net代码不起作用为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码无法正常工作是什么问题:

the following code is not working what is wrong with it:

public class Class1
{
    private int _Event;
    private int _EmpId;
    private string _EmpName;
    private int _Salary;
    private string _City;
    private int _Atype;
    private static readonly string _connectionstring;
    public int Event
    {
        get { return _Event; }
        set { _Event = value; }
    }
    public int EmpId
    {
        get { return _EmpId; }
        set { _EmpId = value; }
    }
    public string EmpName
    {
        get { return _EmpName; }
        set { _EmpName = value; }
    }
    public int Salary
    {
        get { return _Salary; }
        set { _Salary = value; }
    }
    public string City
    {
        get { return _City; }
        set { _City = value; }
    }
    public int Atype
    {
        get { return _Atype; }
        set { _Atype = value; }
    }

    string ss = "";
    public void open()
    {
        String constr = ConfigurationManager.ConnectionStrings["testing"].ConnectionString;
        SqlConnection connect = new SqlConnection(constr);
        
    }
         
   	public void save()
	{

        
        SqlCommand command = new SqlCommand("spdatabaseconnect",connect);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@event", SqlDbType.TinyInt).Value = Atype;
        command.Parameters.Add("@EmpId", SqlDbType.Int).Value = EmpId ;
        command.Parameters.Add("@Emp_name", SqlDbType.NVarChar,50).Value = EmpName ;
        command.Parameters.Add("@Salary", SqlDbType.Int).Value = Salary ;
        command.Parameters.Add("@City", SqlDbType.NVarChar,50).Value = City ;
        connect.Open();
        command.ExecuteNonQuery();
        command.Connection.Close();
        command.Dispose(); 
        connect.Close();
       
    }


这是我的班级文件代码.
单击按钮的代码是:


This my class file code .
The code for button click is :

Class1 obj = new Class1();
    obj.EmpId = Convert.ToInt32(txtEmpId.Text);
     obj.EmpName = txtEmpName.Text;
     obj.Salary = Convert.ToInt32(txtSalary.Text);
     obj.City = txtCity.Text;
     obj.Atype = Convert.ToInt32(txtEvent.Text);
     obj.save();


我想将连接字符串,连接和命令对象写在open()中.
在保存方法中,我只想具有参数.该怎么办?例如,如果我想在编写object.open()时打开连接,则执行open方法中的代码.


I want to write the connection string,connection and command object to be in open().
In save method i only want to have parameters . what to do? eg if i want to open a connection when i write object.open() then the code in open method executes.

推荐答案

尝试添加
SqlConnection connect;

或更改您的打开函数以返回SqlConnection

at the top of your class or changing your open function to return the SqlConnection

public SqlConnection open()
    {
        String constr = ConfigurationManager.ConnectionStrings["testing"].ConnectionString;
        connect = new SqlConnection(constr);
        return connect;  //choose one or the other of the methods

    }


,您应该合并open()和save()方法.
试试这个:
you should merge your open() and save() methods.
try this:
public void openAndSave()
    {
        String constr = ConfigurationManager.ConnectionStrings["testing"].ConnectionString;
        SqlConnection connect = new SqlConnection(constr);        
        SqlCommand command = new SqlCommand("spdatabaseconnect",connect);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@event", SqlDbType.TinyInt).Value = Atype;
        command.Parameters.Add("@EmpId", SqlDbType.Int).Value = EmpId ;
        command.Parameters.Add("@Emp_name", SqlDbType.NVarChar,50).Value = EmpName ;
        command.Parameters.Add("@Salary", SqlDbType.Int).Value = Salary ;
        command.Parameters.Add("@City", SqlDbType.NVarChar,50).Value = City ;
        connect.Open();
        command.ExecuteNonQuery();
        command.Connection.Close();
        command.Dispose(); 
        connect.Close();
       
    }



另外,还需要检查其他一些内容.
1.确保您的连接字符串正确.
2.确保您正在调用的存储过程存在

如果需要分别使用save()和open()方法,请参阅解决方案#1,并在按钮中进行此更改,然后单击



Also here are some other things you need to check.
1. make sure your connection string is correct.
2. make sure that the stored procedure you are calling exists

If you need to have save() and open() methods separately please refer to solution #1 and make this change in your button click

Class1 obj = new Class1();
       obj.EmpId = Convert.ToInt32(txtEmpId.Text);
        obj.EmpName = txtEmpName.Text;
        obj.Salary = Convert.ToInt32(txtSalary.Text);
        obj.City = txtCity.Text;
        obj.Atype = Convert.ToInt32(txtEvent.Text);
        obj.open();
        obj.save();


这篇关于ado.net代码不起作用为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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