Как мне перенести импортированные данные из datagridview в MS sql
В пустой datagridview я получил данные из Excel таблицы и хочу его сохранить в таблице. Как мне это сделать?
Вот сам код Form:
public partial class Form3 : Form
{
//Подключение к бд
SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-CG70ETG; Initial Catalog=wsrdzudo; Integrated Security=True;");
//Запрос
string query = "Select * From Participants";
SqlDataAdapter adapter;
public Form3()
{
InitializeComponent();
adapter = new SqlDataAdapter(query, con);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
DataTable dt = new DataTable();
adapter.Fill(dt);
dataGridView1.DataSource = dt;
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
//Импорт данных из Ecxel
OpenFileDialog opf = new OpenFileDialog();
opf.Filter = "Excel File(*.xlsx;*.xls)|*.xlsx;*.xls|All Files(*.*)|*.*";
opf.ShowDialog();
DataTable tb = new DataTable();
string filename = opf.FileName;
string ConStr = String.Format("Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}; Extended Properties=Excel 12.0;", filename);
System.Data.DataSet ds = new System.Data.DataSet("EXCEL");
OleDbConnection con = new OleDbConnection(ConStr);
con.Open();
DataTable schemaTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
string sheet1 = (string)schemaTable.Rows[0].ItemArray[2];
string select = String.Format("SELECT * FROM [{0}]", sheet1);
OleDbDataAdapter ad = new OleDbDataAdapter(select, con);
ad.Fill(ds);
tb = ds.Tables[0];
con.Close();
dataGridView1.DataSource = tb;
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
adapter.Update((DataTable)dataGridView1.DataSource);
}
private void button3_Click(object sender, EventArgs e)
{
//Подключение к бд
string connectionString = @"Data Source=DESKTOP-CG70ETG; Initial Catalog=wsrdzudo; Integrated Security=True;";
using (SqlConnection con = new SqlConnection(connectionString))
{
//Запрос на удаление
SqlCommand com = new SqlCommand("DELETE FROM Participants WHERE Participants_id= @id", con);
int id = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
com.Parameters.AddWithValue("@id", id);
con.Open();
try
{
com.ExecuteNonQuery();
MessageBox.Show("Запись удалена");
}
catch
{
MessageBox.Show("Удалить не удалось!");
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
dataGridView1.Rows[i].Selected = false;
for (int j = 0; j < dataGridView1.ColumnCount; j++)
if (dataGridView1.Rows[i].Cells[j].Value != null)
if (dataGridView1.Rows[i].Cells[j].Value.ToString().Contains(textBox1.Text))
{
dataGridView1.Rows[i].Selected = true;
break;
}
}
}
private void Form3_Load(object sender, EventArgs e)
{
// TODO: данная строка кода позволяет загрузить данные в таблицу "wsrdzudoDataSet.Participants". При необходимости она может быть перемещена или удалена.
this.participantsTableAdapter.Fill(this.wsrdzudoDataSet.Participants);
}
}