C#连接到sql server2008数据库的实例代码
程序员文章站
2023-12-16 13:21:16
废话不多说了,直接给大家贴代码了,具体代码如下所示:
namespace myfirstapp
{
class program
{
sta...
废话不多说了,直接给大家贴代码了,具体代码如下所示:
namespace myfirstapp { class program { static void main(string[] args) { sqlconnection conn = null; sqlcommand comm = null; sqldatareader sdreader = null; try { string constr = "server=192.168.1.110;uid=sa;pwd=woaifr0828;database=shudiansuo"; conn = new sqlconnection(constr); conn.open(); string sql = "select * from dbo.member"; comm = new sqlcommand(sql); comm.connection = conn; sdreader = comm.executereader(); sdreader.read(); console.writeline("数据库连接成功!"); console.writeline("student表的内容"); while (sdreader.read()) { console.writeline("sdreader[\"id\"]=" + sdreader["id"]); console.writeline("sdreader[\"name\"]=" + sdreader["membername"]); console.writeline("sdreader[\"sex\"]=" + sdreader["phonenum"]); } sdreader.close(); conn.close(); } catch (sqlexception e) { console.writeline("a sqlexception was thrown"); console.writeline("number = " + e.number); console.writeline("message = " + e.message); console.writeline("stacktrace:\n" + e.stacktrace); } console.readkey(); } } }
总结
以上所述是小编给大家介绍的c#连接到sql server2008数据库的实例代码,希望对大家有所帮助