gpt4 book ai didi

c# - 如何在 C# 中保存此表?

转载 作者:太空宇宙 更新时间:2023-11-03 15:20:37 24 4
gpt4 key购买 nike

我在数据库中保存表时遇到问题。我是 SQL 的新手,我第一次使用 2 个表。结果显示在 DataGridView 中,但无法将它们保存到数据库中。我尝试了很多方法,但我无法将 Datagrid 保存到数据库中。

基本上,我会尝试将 DataGridView3 的结果导出到另一个表中,以使它们显示在 Form2 上的报告中。

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace TestDB2
{
public partial class Form1 : Form
{

Form2 fr2 = new Form2();

SqlConnection cn = new SqlConnection();
// SqlCommand cmd = new SqlCommand();
SqlDataReader dr;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

cn.ConnectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;Connect Timeout=30";
// TODO: This line of code loads data into the 'database1DataSet.Table1' table. You can move, or remove it, as needed.
this.table1TableAdapter.Fill(this.database1DataSet.Table1);
// TODO: This line of code loads data into the 'database1DataSet.Table2' table. You can move, or remove it, as needed.
this.table2TableAdapter.Fill(this.database1DataSet.Table2);
textBox1.Text = monthCalendar1.TodayDate.ToString();


}

private void button1_Click(object sender, EventArgs e)
{
// timer1.Enabled = true;

database1DataSet.Table2.Clear();


try
{


dataGridView2.Rows.Clear();
// cmd.CommandText = "Select [First Last Name] from Custumers where Arrival=@arr";
SqlCommand cmd = new SqlCommand("Select * from Table1 where Date2 >= @arr");
cmd.Connection = cn;
cmd.Parameters.AddWithValue("@arr", monthCalendar1.SelectionStart.Date);
cmd.Parameters.AddWithValue("@arr1", monthCalendar1.SelectionStart.Date.AddDays(1));
cn.Open();
dr = cmd.ExecuteReader();

if (dr.HasRows)
{

while (dr.Read())
{

dataGridView2.Rows.Add(dr[0].ToString(), dr[1].ToString(), dr[2].ToString(), dr[3].ToString());


int i = Convert.ToInt32(dr[0]);
string q = Convert.ToString(dr[1]);
DateTime w = Convert.ToDateTime(dr[2]);
DateTime r = Convert.ToDateTime(dr[3]);
this.database1DataSet.Table2.Rows.Add(i, q, w, r);

// this.database1DataSet.Table2.AddTable2Row(i, q, w, r).AcceptChanges();
// this.database1DataSet.Table2.GetChanges();

// cmd.CommandText = "INSERT into Table2 (Id,Name,Date1,Date2) VALUES (@ar,@ar1,@ar2,@ar3)";

}



}
dr.Close();



/*
for (int i = 0; i < dataGridView2.Rows.Count; i++)
{
cmd.CommandText = @"INSERT INTO Table2 (Id) VALUES ('" + dataGridView2.Rows[i].Cells["ID"].Value + "')";
cmd.ExecuteNonQuery();
}
this.Validate();
*/
this.Validate();
cn.Close();
// textBox1.Text = dataGridView3.Rows[2].Cells[1].Value.ToString();
// fr2.ShowDialog();

}
catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); cn.Close(); }
}

private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
textBox1.Text = monthCalendar1.SelectionStart.Date.ToString();
}

private void timer1_Tick(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{

try
{
string sql = "INSERT INTO Table2 (Id, Name, Date1, Date2) VALUES (@A, @B, @C, @D)";
using (SqlConnection conn = cn)
{
conn.Open();
for (int i = 0; i < dataGridView3.Rows.Count; i++)
{

SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@A", dataGridView3.Rows[i].Cells[0].Value.ToString());
cmd.Parameters.AddWithValue("@B", dataGridView3.Rows[i].Cells[1].Value.ToString());
cmd.Parameters.AddWithValue("@C", dataGridView3.Rows[i].Cells[2].Value.ToString());
cmd.Parameters.AddWithValue("@D", dataGridView3.Rows[i].Cells[3].Value.ToString());
cmd.ExecuteNonQuery();

}
conn.Close();
}


}
catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }

}
}
}

enter image description here

最佳答案

努力改变

      cmd.Parameters.AddWithValue("@A", dataGridView3.Rows[i].Cells[0].Value.ToString());

     cmd.Parameters.AddWithValue("@A", dataGridView3.Rows[i].Cells[0].Text);

关于c# - 如何在 C# 中保存此表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37517032/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com