我该如何纠正? [英] how do i rectify this?

查看:80
本文介绍了我该如何纠正?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace final2
{
    public partial class Form1 : Form
    {
        private SqlConnection con;
        private SqlCommand command;
        private SqlDataAdapter adapter;
        private DataSet dataset;
        public DataGridView dg;

        public Form1()
        {
            InitializeComponent();

            con = new SqlConnection();
            command = con.CreateCommand();
            con.ConnectionString = "Data Source=CASSINI-003-PC;Initial Catalog=studentdb;Integrated Security=True";
            adapter = new SqlDataAdapter(command);
            dataset = new DataSet();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            command.Parameters.Clear();
            command.Parameters.AddWithValue("@StudentID", textBox1.Text);
            command.Parameters.AddWithValue("@Name", textBox2.Text);
            command.Parameters.AddWithValue("@Age", textBox3.Text);
            command.Parameters.AddWithValue("@Gender", textBox4.Text);
            command.Parameters.AddWithValue("@Courseno", listBox1.SelectedItem);
            command.CommandText = "INSERT into details" + "(StudentID,Name,Age,Gender,Courseno)VALUES" + "(@StudentID,@Name,@Age,@Gender,@Courseno)";

            try
            {
                con.Open();

                int result = command.ExecuteNonQuery();

                if (result > 0)
                    MessageBox.Show("student successfully updated");
                else
                    MessageBox.Show("failed to update");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }

            ClearFields();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            command.Parameters.Clear();
            command.Parameters.AddWithValue("@StudentID", textBox1.Text);
            command.CommandText = "SELECT * FROM details WHERE StudentID=@StudentID";

            dataset.Tables.Clear();

            int result = adapter.Fill(dataset, "details");

            if (result > 0)
            {
                DataRow srow = dataset.Tables["details"].Rows[0];
                textBox1.Text = srow["StudentID"].ToString();
                textBox2.Text = srow["Name"].ToString();
                textBox3.Text = srow["Age"].ToString();
                textBox4.Text = srow["Gender"].ToString();
                listBox1.SelectedItem = srow["Courseno"].ToString();

            }
            else
            {
                MessageBox.Show("Student does not exist");
            }
       
        }

        void ClearFields()
        {
            textBox1.Text = String.Empty;
            textBox2.Text = String.Empty;
            textBox3.Text = String.Empty;
            textBox4.Text = String.Empty;
            

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string currentitem = listBox1.SelectedItem.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            command.Parameters.Clear();
            command.Parameters.AddWithValue("@Courseno", listBox1.SelectedValue);
            command.CommandText = "SELECT * FROM results WHERE Courseno=@Courseno";

            try
            {
                con.Open();
                command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
          
            adapter.Fill(dataset);
            dataGridView1.DataSource = dataset;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'studentdbDataSet1.result' table. You can move, or remove it, as needed.
            this.resultTableAdapter.Fill(this.studentdbDataSet1.result);

        }

        
    }
}

抛出的异常是查询期望未提供参数@Courseno.

the exception thrown is that the query expects the parameter @Courseno which was not supplied.

推荐答案

您的问题似乎是您尝试设置的方式修改您的代码.初始化表单时,需要设置适配器并传递命令对象.稍后,您将信息放入该命令对象中,但从未将其传递回适配器中.我建议您从一个不太复杂的例子开始.现在很难看到何时何地发生了什么.而是将所有连接,命令,适配器等对象放入您的方法中.为此呼叫创建新的呼叫.按照应有的方式设置所有内容并运行您的呼叫.那应该工作.然后,我建议慢慢地恢复到您要尝试建立的模型.

附带说明一下,我个人不喜欢连接对象的类级变量.这将在类的整个生存期内使连接保持打开状态.这将使用几乎在每种情况下都不必要的资源.我通常会尝试使与数据库的连接的持续时间尽可能短.如果我要一次执行多个操作,则将在该组操作的整个生命周期内保持打开状态,但这是我有一次打开操作的最长时间.只是需要考虑的事情.
Your problem seems to be how you are trying to set up your code. When you initialize your form, you set up your adapter and passed in the command object. Later you put information into that command object but never passed it back into the adapter. I would recommend that you start with a less complicated example. Right now it is difficult to see what is going where and when. Instead, put all of the connection, command, adapter, etc. objects into your method. Create new ones just for that call. Set everything up the way it should be and run your call. That should work. Then I would recommend slowly working back to the model you are trying to get to.

As a side note, I personally am not a fan of class-level variables for connection objects. This leaves the connection open for the duration of the class''s lifetime. This will use more resources than is necessary in almost every scenario. I usually try to keep the connections to the database as short in duration as possible. If I''m going to be doing a number of operations at once, I''ll keep it open for the lifespan of that set of operations but that is the longest I ever keep one open. Just something to think about.


private void button3_Click(object sender, EventArgs e)
       {

           //command.Parameters.AddWithValue("@Courseno", listBox1.SelectedValue);
              
           command.CommandText = "SELECT * FROM results WHERE Courseno = " + listBox1.SelectedValue;

           adapter.Fill(dataset);
           dataGridView1.DataSource = dataset;
       }


它就像..虽然这不是一个好过程..但是可以至少消除您的错误..
it will be like..though it isn''t a good process..but can remove your error atleast..
command.CommandText = "SELECT * FROM results WHERE Courseno = ''" + listBox1.SelectedValue + "''";


这篇关于我该如何纠正?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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