vs2005 + MS SQL2000 ,初学vs2005,什么都不懂 在vs2005中访问数据库ms sql2000,查询表 table1中的数据,怎么实现: 查到需要的数据时就提示成功,查不到需要的数据时提示查询无此结果??
……
……
using System.Data.SqlClient;namespace sqll
{
public partial class Form1 : Form
{
string sqlstring; //定义SQL语句字符串 private void DataBind() //数据库访问
{
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = "server=localhost;uid=sa;pwd=;database=db_msdb1";
sqlcon.Open();
SqlCommand sqlcom = new SqlCommand(sqlstring,sqlcon);
SqlDataAdapter sqlad = new SqlDataAdapter(sqlcom);
DataTable datab = new DataTable();
sqlad.Fill(datab);
dataGridView1.DataSource = datab;
} public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
sqlstring = "select * from table1 where stname='scofield'"; DataBind(); //这里怎么写提示: 查询到table1 需要的结果就提示成功,否则提示查询无此结果
}
}
} 还有,访问数据库的过程,应该怎样写才最合理?? 怎么写可以让 select、insert、update、delete可以在同一个过程中完成 ?
初学c#,请各位高手帮帮忙
……
……
using System.Data.SqlClient;namespace sqll
{
public partial class Form1 : Form
{
string sqlstring; //定义SQL语句字符串 private void DataBind() //数据库访问
{
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = "server=localhost;uid=sa;pwd=;database=db_msdb1";
sqlcon.Open();
SqlCommand sqlcom = new SqlCommand(sqlstring,sqlcon);
SqlDataAdapter sqlad = new SqlDataAdapter(sqlcom);
DataTable datab = new DataTable();
sqlad.Fill(datab);
dataGridView1.DataSource = datab;
} public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
sqlstring = "select * from table1 where stname='scofield'"; DataBind(); //这里怎么写提示: 查询到table1 需要的结果就提示成功,否则提示查询无此结果
}
}
} 还有,访问数据库的过程,应该怎样写才最合理?? 怎么写可以让 select、insert、update、delete可以在同一个过程中完成 ?
初学c#,请各位高手帮帮忙
首先 字符或字符串_查询语句
再分析该字符串 前一个字符串约定是什么代表Select Update 或者其他类似
然后根据前面 执行后面的SQL语句
using System.Data.SqlClient;namespace sqll
{
public partial class Form1 : Form
{
string sqlstring; //定义SQL语句字符串 private boolDataBind() //数据库访问
{
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = "server=localhost;uid=sa;pwd=;database=db_msdb1";
try{
sqlcon.Open();
}
catch
{
return false;
}
SqlCommand sqlcom = new SqlCommand(sqlstring,sqlcon);
try{ SqlDataAdapter sqlad = new SqlDataAdapter(sqlcom);
DataTable datab = new DataTable();
sqlad.Fill(datab);
dataGridView1.DataSource = datab;
if(dataGridView1.Rows.Count>0)
return true;
return false}
catch{return false;}
}
return false; public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
sqlstring = "select * from table1 where stname='scofield'"; if(DataBind()){
MessageBox.Show("成功", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
};
else
MessageBox.Show("失败", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;//这里怎么写提示: 查询到table1 需要的结果就提示成功,否则提示查询无此结果
}
}
}
{
public Form1()
{
InitializeComponent();
} string sqlstring; //定义SQL语句字符串 private bool DataBind() //数据库访问
{
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = "server=localhost;uid=sa;pwd=;database=db_msdb1";//这里既然都给出链接字符串了 上面还定义那个干什么
try
{
sqlcon.Open();
}
catch
{
return false;
}
SqlCommand sqlcom = new SqlCommand(sqlstring, sqlcon);
try
{
SqlDataAdapter sqlad = new SqlDataAdapter(sqlcom);
DataTable datab = new DataTable();
sqlad.Fill(datab); if (datab.Rows.Count > 0)
{
dataGridView1.DataSource = datab;
return true;
}
return false;
}
catch
{
return false;
}
}
private void button1_Click(object sender, EventArgs e)
{
sqlstring = "select * from table1 where stname='scofield'"; if (DataBind())
{
MessageBox.Show("成功", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
MessageBox.Show("失败", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
} }
}
SqlDataReader reader = 执行ExecuteReader方法
DataTable table = new Datable();
table.load(reader);if(判断table的rows)
{
成功
}
else
{
失败
}insert delete update 都可以写在一个函数内
只是sql语句不同 执行函数都可以用ExecuteNoquery();找找SqlHelper帮助类 微软官方的 里面操作数据库的代码都有
string sqlStr="";//SQL语句
OracleCommand cmd = new OracleCommand(sqlStr, Conn);//conn为数据库连接语句
int r = cmd.ExecuteNonQuery();
if (r >= 1)
{
messagebox.show("查询到数据!");
}
else
{
MessageBox.Show("未找到数据!");
}
private Bool DataBind() //数据库访问
{ Bool datavalue=false;
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = "server=localhost;uid=sa;pwd=;database=db_msdb1";
SqlCommand sqlcom = new SqlCommand(sqlstring,sqlcon);
SqlDataAdapter sqlad = new SqlDataAdapter(sqlcom);
DataTable datab = new DataTable();
if(datab.Rows.count!=0)
{
sqlad.Fill(datab);
dataGridView1.DataSource = datab;
datavalue=true;
} else{datavalue=false;}
return datavalue;
}
调用时:
if (DataBind())
{
MessageBox.Show("成功", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("失败", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}