C#如何登陆数据库
- 提问者网友:那叫心脏的地方装的都是你
- 2021-04-27 09:54
- 五星知识达人网友:轮獄道
- 2021-04-27 10:08
这个需要 具体的连接字符串 我自己定义一个.config自述文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="ChatDatabase" connectionString="Data Source=.\SQLEXPRESS;
 AttachDbFilename=C:\Users\jrguo\Desktop\课程设计资料库\TB Server\ChatData.mdf;
 Integrated Security=True;
 User Instance=True" />
<add name="TB_Server.Properties.Settings.ChatDataConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ChatData.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
然后呢 比如下面的是检测账号的一段代码
public static string checkNumber(string Number)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ChatDatabase"].ConnectionString);
string query = "SELECt Number FROM Table1 ";
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
if (reader[0].ToString() == Number)
{
conn.Close();
return "false";
}
}
conn.Close();
return "true";
}
catch
{
return "false";
}
}