private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn;//打开数据库连接字符串
SqlDataAdapter da;//执行数据库的命令字符串
string strconn = "Data Source=ZL;Initial Catalog=LED;User=sa;pwd=";
//连接数据库
try
{
if (txt1.Text !=null)//第一个TEXT录入
{
if (txt2.Text !=null)//第二个
{
if (txt3.Text != "")//第三个
{
if (txt4.Text != "")//第四个
{
conn = new SqlConnection(strconn);//实例数据库
conn.Open();//打开数据库
da = new SqlDataAdapter(strconn, conn);
//da.Fill();
SqlCommand sqd = conn.CreateCommand();
sqd.CommandText = ("insert into UserInformation values('"+txt1.Text+"',"+txt2.Text+"," + txt3.Text +","+"'" +txt4.Text+"'"+")");
MessageBox.Show("数据添加成功"+MessageBoxButtons.OK);
Form2 form2 = new Form2();
form2.Close(); /* public class frm1class : dbconn
{
SqlConnection conn = new SqlConnection();
SqlDataAdapter sqd = new SqlDataAdapter();
SqlCommand sqc = new SqlCommand();
string strconn = "Data Source=ZL;Initial Catalog=LED;User=sa;pwd=";
public frm1class()
: base(DataTable)
{
sqd = conn(strconn);
conn.Open();
sqc = conn.CreateCommand();
sqc = "select * from UserInformation";
conn.Close();
} }*/
return frm1class();
Form1 form1 = new Form1();
form1.Show();
//da.InsertCommand.CommandText = sql;
sqd.ExecuteNonQuery();
sqd.Clone();
conn.Close();
//将数据库变量附值并执行
}
else
{
MessageBox.Show("数据不完整" + MessageBoxButtons.OKCancel);
}
}
else
{
MessageBox.Show("数据不完整" + MessageBoxButtons.OKCancel);
}
}
else
{
MessageBox.Show("数据不完整" + MessageBoxButtons.OKCancel);
}
}
else
{
MessageBox.Show("数据不完整" + MessageBoxButtons.OKCancel);
}
}
catch (Exception err)
{
MessageBox.Show("插入数据库记录失败" + err.ToString());
}
}我想把/**/内的数据加载到类里~请教怎么做.
{
SqlConnection conn;//打开数据库连接字符串
SqlDataAdapter da;//执行数据库的命令字符串
string strconn = "Data Source=ZL;Initial Catalog=LED;User=sa;pwd=";
//连接数据库
try
{
if (txt1.Text !=null)//第一个TEXT录入
{
if (txt2.Text !=null)//第二个
{
if (txt3.Text != "")//第三个
{
if (txt4.Text != "")//第四个
{
conn = new SqlConnection(strconn);//实例数据库
conn.Open();//打开数据库
da = new SqlDataAdapter(strconn, conn);
//da.Fill();
SqlCommand sqd = conn.CreateCommand();
sqd.CommandText = ("insert into UserInformation values('"+txt1.Text+"',"+txt2.Text+"," + txt3.Text +","+"'" +txt4.Text+"'"+")");
MessageBox.Show("数据添加成功"+MessageBoxButtons.OK);
Form2 form2 = new Form2();
form2.Close(); /* public class frm1class : dbconn
{
SqlConnection conn = new SqlConnection();
SqlDataAdapter sqd = new SqlDataAdapter();
SqlCommand sqc = new SqlCommand();
string strconn = "Data Source=ZL;Initial Catalog=LED;User=sa;pwd=";
public frm1class()
: base(DataTable)
{
sqd = conn(strconn);
conn.Open();
sqc = conn.CreateCommand();
sqc = "select * from UserInformation";
conn.Close();
} }*/
return frm1class();
Form1 form1 = new Form1();
form1.Show();
//da.InsertCommand.CommandText = sql;
sqd.ExecuteNonQuery();
sqd.Clone();
conn.Close();
//将数据库变量附值并执行
}
else
{
MessageBox.Show("数据不完整" + MessageBoxButtons.OKCancel);
}
}
else
{
MessageBox.Show("数据不完整" + MessageBoxButtons.OKCancel);
}
}
else
{
MessageBox.Show("数据不完整" + MessageBoxButtons.OKCancel);
}
}
else
{
MessageBox.Show("数据不完整" + MessageBoxButtons.OKCancel);
}
}
catch (Exception err)
{
MessageBox.Show("插入数据库记录失败" + err.ToString());
}
}我想把/**/内的数据加载到类里~请教怎么做.
SqlConnection conn = new SqlConnection("server=127.0.0.1;database=Info;uid=sa;pwd=sa;");
conn.Open();
SqlCommand cmd = new SqlCommand(SelectUrl,conn);
myReader = cmd.ExecuteReader();
while(myReader.Read())
{
XX = (myReader.GetString(0));
................
}可以这样
{XX = (myReader.GetString(0));
................
}在myReader.Read()中使用GetString(0)XX的值是附值给谁的?
myReader.GetString(i) myReader.GetString(i) 就是你查询语句 SelectUrl 中的字段对应的值
i 从0开始,其他的就你自己数了..你愿意把这个值赋值给谁都行,所以用了XX