大二新手 打算写一个关于图书管理系统的程序但是在录入信息时遇到困难不知道C#要怎么连接数据库 求大神指教
这个问题问的过于宽泛
如果是网站,首先在WEB.CONFIG里配置数据库连接字符串,以oracle为例(其它数据库请百度)
然后,在网页的后台使用这个连接去数据库取数据
OracleConnection theConn = new OracleConnection();
theConn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStrRmesuser"].ToString();
OracleCommand theComd = new OracleCommand();
theComd.Connection = theConn;
string theSql = thisSql;
theComd.CommandText = theSql;
OracleDataAdapter theDa = new OracleDataAdapter();
DataSet theDs = new DataSet();
theSql = thisSql;
theComd.CommandText = theSql;
theDa.SelectCommand = theComd;
theDa.Fill(theDs);
theConn.Close();
DataTable dt = theDs.Tables[0]; //这就是你要的结果集
这是web.config里的配置,刚才发了一遍竟然消失了,再发一遍
为什么不让我发配置。。。
先在前面写using System.Data.SqlClient;
然后在对应的位置写数据库连接的代码:比如连接SQL,代码如下:
SqlConnection sqlConnection=new SqlConnection();
sqlConnection.ConnectionString="Server=(local);Database=数据库名称;Integrated Security=sspi";
命名空间引用using System.Data.SqlClient;
如果你用的是微软的sql的话这个是必须的
SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接;
sqlConnection.ConnectionString =
"Server=(Local);Database=EduBaseDemo;Integrated Security=sspi"; //在字符串变量中,描述连接字符串所需的服务器地址、数据库名称、集成安全性(即是否使用Windows验证),这里用了Windows身份验证;
sqlConnection.Open();
这里放你的数据库操作语句
sqlConnection.Close();
如果需要演示,需要经常更改配置的话可以把连接字符串放到配置文件里,再调用
如果有帮助请点击采纳~O(∩_∩)O谢谢
建议先学习EF6,这个和数据库连接方便强大