首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#winform 如何改造下列代码

2013-03-27 
C#winform 怎么改造下列代码,改造下列代码,将它改造成通过这个代码来连接数据库,其他不变,server176.16.0

C#winform 怎么改造下列代码,
改造下列代码,将它改造成  

通过这个代码来连接数据库,其他不变,  server=176.16.0.33;uid=sa;password=sa;database=PDMIS

以下是源代码
//连接服务器,并获取连接字符串
        private bool ConnectServer()
        {
            try
            {
                Desay.HR.Business.ConnectionString cs = new ConnectionString(txtServer.Text, Convert.ToInt32(numPort.Value));
                connectionString = cs.GetConnectionStringByKey("PDMIS");
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器连接失败!\n失败描述:" + ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            //TcpChannel channel = new TcpChannel();
            //ChannelServices.RegisterChannel(channel, false);

            //string uri = string.Format("tcp://{0}:{1}/DataService", txtServer.Text, numPort.Value);
            //try
            //{

            //    IDataService ds = (IDataService)Activator.GetObject(typeof(DataService), uri);
            //    connectionString = ds.GetDataServer();
            //    return true;
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("服务器连接失败!\n失败描述:" + ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            //    return false;

            //}
            //finally
            //{
            //    ChannelServices.UnregisterChannel(channel);
            //}
        }
------解决方案--------------------


 private bool ConnectServer()
         {
             try
             {
                 //Desay.HR.Business.ConnectionString cs = new ConnectionString(txtServer.Text, Convert.ToInt32(numPort.Value));
                 connectionString = "Data Source=176.16.0.33;Initial Catalog=PDMIS;Persist Security Info=True;User ID=sa;Password=sa;"
                 return true;
             }
             catch (Exception ex)
             {
                 MessageBox.Show("服务器连接失败!\n失败描述:" + ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                 return false;
             }
 
[解决办法]

    private bool ConnectServer()
        {
            try
            {
                Desay.HR.Business.ConnectionString cs = new ConnectionString("176.16.0.33;uid=sa;password=sa;database=PDMIS
", Convert.ToInt32(numPort.Value));
                connectionString = cs.GetConnectionStringByKey("PDMIS");
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("服务器连接失败!\n失败描述:" + ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return false;
            }

            //TcpChannel channel = new TcpChannel();
            //ChannelServices.RegisterChannel(channel, false);

            //string uri = string.Format("tcp://{0}:{1}/DataService", txtServer.Text, numPort.Value);
            //try
            //{

            //    IDataService ds = (IDataService)Activator.GetObject(typeof(DataService), uri);


            //    connectionString = ds.GetDataServer();
            //    return true;
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("服务器连接失败!\n失败描述:" + ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            //    return false;

            //}
            //finally
            //{
            //    ChannelServices.UnregisterChannel(channel);
            //}
        }


[解决办法]
   SqlConnection sqlConn = new SqlConnection("="Data Source=176.16.0.33; Initial Catalog=PDMIS; User ID=sa; Password=sa;");
 


 
[解决办法]
应该在  ConnectionString 类中,如果你改不了,就用1楼的

connectionString = "Data Source=176.16.0.33;Initial Catalog=PDMIS;Persist Security Info=True;User ID=sa;Password=sa;"

热点排行