110,499
社区成员
发帖
与我相关
我的任务
分享
OleDbConnection ocon;
DataSet ds = new DataSet();
OleDbDataAdapter oda ;
private void button1_Click(object sender, EventArgs e)
{
OleDbCommandBuilder al = new OleDbCommandBuilder(oda);
//表里面一定要有主键
oda.Update(ds);
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
private void button2_Click(object sender, EventArgs e)
{
ocon = new OleDbConnection(strconn);
ocon.Open();
string sql = "select * from UserInfo";
oda = new OleDbDataAdapter(sql,ocon);
oda.Fill(ds);
[ this.dataGridView1.DataSource = ds.Tables[0].DefaultView;]
}
private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
MessageBox.Show("输入完行了");
}
private void dataGridView1_NewRowNeeded(object sender, DataGridViewRowEventArgs e)
{
}
private void dataGridView1_AllowUserToAddRowsChanged(object sender, EventArgs e)
{
}
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
}
private void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
MessageBox.Show("输入完行了");
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
try
{
SqlConnection con = DB.CreateCon();
con.Open();
string[] test ={ "a", "b", "c" };
string[] arr ={"1","2","3" };
string[] ok ={"m","n","o" };
String SqlStr = "select * from test";
//新建内存表dt
DataTable dt = new DataTable("test");
dt.Columns.Add("test", System.Type.GetType("System.String"));
dt.Columns.Add("arr", System.Type.GetType("System.String"));
dt.Columns.Add("ok", System.Type.GetType("System.String"));
//增加数组里的数据
for (int i = 0; i < arr.Length; i++)
{
DataRow row = dt.NewRow();
row["test"] = test[i];
row["arr"] = arr[i];
row["ok"] = ok[i];
dt.Rows.Add(row);
//显示当前行的状态
Console.Write(row[0].ToString());
}
//建立sql适配器
SqlDataAdapter dap = new SqlDataAdapter(SqlStr,con);
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(dap);
//建立sql数据集 并且更新数据库
DataSet ds = new DataSet();
ds.Tables.Add(dt);
int val = dap.Update(ds, "test");
ds.AcceptChanges();
}
catch(SqlException ex)
{
Console.Write(ex.Errors);
}
}
}
class DB
{
public static SqlConnection CreateCon()
{
return new SqlConnection("server=WWW-16EA86D80FF;uid=sa;Pwd=1;database=test");
}
}
}
//this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
这句不加进去就是不想让别人看到里面的东西
adapter.InsertCommand = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection);
像这样子也不是动态生成sql语句啊