为什么我的c#windows形式的代码不起作用?

时间:2016-05-15 04:35:49

标签: c#

为什么我的网格视图不会刷新数据?它添加到数据库,但然后清除我的网格视图,并没有任何内容。它加载表单,我可以在其中输入信息点击添加按钮,它会添加到数据库,但网格视图刷新不会将任何数据放入其中。

private void addButton_Click(object sender, EventArgs e)
    {
        string title = titleTextBox.Text;
        string starring = starringTextBox.Text;
        string year = yearTextBox.Text;
        SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=<PATH TO FILE>);
        conn.Open();

        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "INSERT INTO dbo.Movie (Title, Starring, Year) VALUES (@title, @starring, @year)";            
        cmd.Parameters.AddWithValue("@Title", title);
        cmd.Parameters.AddWithValue("@Starring", starring);
        cmd.Parameters.AddWithValue("@Year", year);

        cmd.Connection = conn;

        cmd.ExecuteNonQuery();

        SqlDataAdapter MyDataAdapter = new SqlDataAdapter(cmd.CommandText, conn);

        titleTextBox.Text = "";
        starringTextBox.Text = "";
        yearTextBox.Text = "";
        titleTextBox.Focus();

        movieTableAdapter.Fill(moviesDataSet.Movie);
        movieBindingSource.DataSource = movieTableAdapter;
        myDataGridView.DataSource = movieBindingSource;
        myDataGridView.Refresh();
        myDataGridView.Update();

        conn.Close();

    }

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

    }

1 个答案:

答案 0 :(得分:0)

import zipfile

localFile = 'beer.zip'

with zipfile.ZipFile(localFile, 'r') as myzip:
    myzip.extractall()
    print 'got:', [info.filename for info in myzip.infolist()]

将选择查询传递给适配器。您正在传递插入查询。