c#编好的程序转移到别的电脑数据库的问题

landry1234 2009-04-22 12:51:29
1. 我编号了一个小程序,有连接数据库的,拷到别的电脑上不能用,我估计是别的电脑的服务器名不一样,这样导致我的连接数据库代码无效,我连接的时候用的是windows身份验证。怎么解决呢?


2. 我们平时使用的很多软件都能保存数据,但客户端也不用装数据库就能实现保存数据,但我编的程序就一定要装数据库才能用,这是为什么呢?希望各位能解除我心中的疑问,谢谢
...全文
97 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
abc1_2_3 2009-04-22
  • 打赏
  • 举报
回复
用的什么数据库,SQL、Access、Oracle?连接用windows身份验证,拷到别的电脑上当然不能用
Tensionli 2009-04-22
  • 打赏
  • 举报
回复
如果是单机的话,可以考虑不用数据库,另外检查客户端的数据库连接是否正确.
蓝海D鱼 2009-04-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 landry1234 的回复:]
引用 1 楼 abc1_2_3 的回复:
用的什么数据库,SQL、Access、Oracle?连接用windows身份验证,拷到别的电脑上当然不能用

那用sql server 身份验证可以吗?
[/Quote]
当然可以
WEB.CONFIG
=======================================================
<appSettings>
<add key="ConnectionString" value="server=Develop2;uid=sa;database=AnQuanDB" />
</appSettings>

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace DatabaseOperate
{
class SqlOperateInfo
{
//Suppose your ServerName is "aa",DatabaseName is "bb",UserName is "cc", Password is "dd"
private string sqlConnectionCommand = "Data Source=aa;Initial Catalog=bb;User ID=cc;Pwd=dd";
//This table contains two columns:KeywordID int not null,KeywordName varchar(100) not null
private string dataTableName = "Basic_Keyword_Test";

private string storedProcedureName = "Sp_InertToBasic_Keyword_Test";
private string sqlSelectCommand = "Select KeywordID, KeywordName From Basic_Keyword_Test";
//sqlUpdateCommand could contain "insert" , "delete" , "update" operate
private string sqlUpdateCommand = "Delete From Basic_Keyword_Test Where KeywordID = 1";

public void UseSqlReader()
{
SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand);
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandType = System.Data.CommandType.Text;
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = sqlSelectCommand;

sqlConnection.Open();
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
while(sqlDataReader.Read())
{
//Get KeywordID and KeywordName , You can do anything you like. Here I just output them.
int keywordid = (int)sqlDataReader[0];
//the same as: int keywordid = (int)sqlDataReader["KeywordID"]
string keywordName = (string)sqlDataReader[1];
//the same as: string keywordName = (int)sqlDataReader["KeywordName"]
Console.WriteLine("KeywordID = " + keywordid + " , KeywordName = " + keywordName);
}

sqlDataReader.Close();
sqlCommand.Dispose();
sqlConnection.Close();
}
public void UseSqlStoredProcedure()
{
SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand);
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = storedProcedureName;

sqlConnection.Open();
sqlCommand.ExecuteNonQuery();
//you can use reader here,too.as long as you modify the sp and let it like select * from ....

sqlCommand.Dispose();
sqlConnection.Close();
}
public void UseSqlDataSet()
{
SqlConnection sqlConnection = new SqlConnection(sqlConnectionCommand);
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandType = System.Data.CommandType.Text;
sqlCommand.Connection = sqlConnection;
sqlCommand.CommandText = sqlSelectCommand;

sqlConnection.Open();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();
sqlDataAdapter.SelectCommand = sqlCommand;
DataSet dataSet = new DataSet();
//sqlCommandBuilder is for update the dataset to database
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
sqlDataAdapter.Fill(dataSet, dataTableName);

//Do something to dataset then you can update it to Database.Here I just add a row
DataRow row = dataSet.Tables[0].NewRow();
row[0] = 10000;
row[1] = "new row";
dataSet.Tables[0].Rows.Add(row);

sqlDataAdapter.Update(dataSet, dataTableName);

sqlCommand.Dispose();
sqlDataAdapter.Dispose();
sqlConnection.Close();
}
}
}
资料引用:http://www.knowsky.com/345354.html
landry1234 2009-04-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 abc1_2_3 的回复:]
用的什么数据库,SQL、Access、Oracle?连接用windows身份验证,拷到别的电脑上当然不能用
[/Quote]
那用sql server 身份验证可以吗?
landry1234 2009-04-22
  • 打赏
  • 举报
回复
我用sql 2005
liucuiqiang 2009-04-22
  • 打赏
  • 举报
回复
数据库连接写在配置文件,在别的机器用的时候,修改一下配置即可。
有些软件可以保存数据(没用数据库),它们把数据存在文件或者存在ACCESS或者XML里面都有可能。这些文件一般都放在此软件安装的位置上面。

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧