private void button1_Click(object sender, EventArgs e)
{
SqlConnection myconn = new SqlConnection();
myconn.ConnectionString = "server=(local);database=SQLview;uid=sa;pwd=123";
myconn.Open();
string mysql = "INSERT into 区县表 VALUES('"+textBox1+"','"+textBox2+"','"+textBox3+"','"+textBox4+"','"+textBox5+"','"+textBox6+"','"+textBox7+"','"+textBox8+"')";
SqlCommand mycmd = new SqlCommand(mysql,myconn);
mycmd.CommandText = mysql;
mycmd.ExecuteNonQuery();
MessageBox.Show("数据添加成功!");
SqlDataAdapter AdapterSelect = new SqlDataAdapter("select* from 区县表", myconn);
DataTable dt = new DataTable();
AdapterSelect.Fill(dt);
dataGridView1.DataSource = dt.DefaultView;
}
————————
你这些东西的数据类型呢?
你这串代码实在不咋的
([iIndex]
)
VALUES
(1
)把列名称加上
{
string s1 = textBox1.Text;
string s2 = textBox2.Text;
string s3 = textBox3.Text;
string s4 = textBox4.Text;
string s5 = textBox5.Text;
string s6 = textBox6.Text;
string s7 = textBox7.Text;
string s8 = textBox8.Text;
SqlConnection myconn = new SqlConnection();
myconn.ConnectionString = "server=(local);database=SQLview;uid=sa;pwd=123";
string mysql = "INSERT into 区县表 (代码,省份,隶属,名称,面积(平方公里),人口(万),邮编,区号)VALUES('"+s1+"','"+s2+"','"+s3+"','"+s4+"','"+s5+"','"+s6+"','"+s7+"','"+s8+"')";
SqlCommand mycmd = new SqlCommand(mysql,myconn);
try
{ myconn.Open();
mycmd.ExecuteNonQuery();
myconn.Close();
} catch (Exception exp)
{
Console.WriteLine("{0} Exception caught.", exp);
}
finally
{
MessageBox.Show("数据添加成功!");
}
这样改一下 好想还是不行啊 数据库里没有添加的内容
textBox1控件 怎么当值传进去呢,他是一个控件,你应该把控件所保存的值传进去才行
上面数据类型都没转化过来,比如面积(平方公里)
double s5=Convert.ToDouble(textBox5.Text); //如果类型为double类型
string mysql = "INSERT into 区县表 VALUES('"+textBox1+"','"+textBox2+"','"+textBox3+"','"+textBox4+"','"+textBox5+"','"+textBox6+"','"+textBox7+"','"+textBox8+"')";这句就是错的....后面都加上.Text
后面的“人口(万)”“区号”“邮编 ”都是float的
private void button1_Click(object sender, EventArgs e)
{
string s1 = textBox1.Text;
string s2 = textBox2.Text;
string s3 = textBox3.Text;
string s4 = textBox4.Text;
string s5 = textBox5.Text;
double s6 = double.Parse(textBox6.Text);
double s7 = double.Parse(textBox7.Text);
double s8 = double.Parse(textBox8.Text);
SqlConnection myconn = new SqlConnection();
myconn.ConnectionString = "server=(local);database=SQLview;uid=sa;pwd=123";
string mysql = "INSERT into 区县表 (代码,省份,隶属,名称,面积(平方公里),人口(万),邮编,区号)VALUES('"+s1+"','"+s2+"','"+s3+"','"+s4+"','"+s5+"','"+s6+"','"+s7+"','"+s8+"')";
SqlCommand mycmd = new SqlCommand(mysql,myconn);
try
{ myconn.Open();
mycmd.ExecuteNonQuery();
myconn.Close();
} catch (Exception exp)
{
Console.WriteLine("{0} Exception caught.", exp);
}
finally
{
MessageBox.Show("数据添加成功!");
}
}
string mysql = "INSERT into 区县表 (代码,省份,隶属,名称,面积(平方公里),人口(万),邮编,区号)VALUES('"+s1+"','"+s2+"','"+s3+"','"+s4+"','"+s5+"','"+s6+"','"+s7+"','"+s8+"')";
SqlCommand mycmd = new SqlCommand(mysql,myconn);
中的
SqlCommand mycmd = new SqlCommand(mysql,myconn);
处设置一个断点,运行到该断点,然后在观察容器里,看mysql这个串的值,并将值复制出来。然后粘贴到SQL Server Management Studio的查询窗口,看能否执行。另外提个建议,把MessageBox写在finally子句里,实在不是个好习惯。finally里一般放资源释放类的操作。在这里,我宁肯你放的是:
myconn.Close();