如何在客户端PC上运行Windows Form SQL Server Express基于程序 [英] How do I run windows form SQL server express based program on client pc

查看:196
本文介绍了如何在客户端PC上运行Windows Form SQL Server Express基于程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我好难受
我在Visual Studio 2017中使用sql express创建了一个Windows窗体.
该应用程序在我自己的PC上运行良好,但是当我发布它并安装在另一台PC上时,出现错误,这是与网络相关的错误52. 考虑一下我已经在clinet pc上安装了sql express.
此链接到我的项目

Hamkar_Vendors-下载-4shared [

hello guys i get stuck so badly
i created a windows form with sql express with visual studio 2017 .
the application work nicely in my own pc but when i published it and instal it another pc , i get error a network related error 52.
consider it that i have installed sql express on clinet pc.
this link to my project

Hamkar_Vendors - Download - 4shared[^]

this my code

using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Hamkar_Vendors
    {
        public partial class MainForm : Form
        {
            //static string path = Path.GetFullPath(Environment.CurrentDirectory);
            //static string databaseName = "Hamkar_Vendor.mdf";
            string con_string = @"Data Source=.\SQLEXPRESS2014;Initial Catalog=hamkar_Vendor1;Integrated Security=True";
    
            SqlDataAdapter dataAdapter;
            DataTable dataTable;
            public MainForm()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                vendorGrp.Hide();
                Getdata("select * from vendor");
                
    
    
            }
    
            private void Getdata(string selectedString)
            {
                try { 
                dataAdapter = new SqlDataAdapter(selectedString,con_string );
                dataTable = new DataTable();
                dataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;
                dataAdapter.Fill(dataTable);
                bindingSource1.DataSource = dataTable;
                    }
                catch(Exception e)
                {
                    MessageBox.Show(e.Message);
                }
            }
    
            private void ثبتاطلاعاتToolStripMenuItem_Click(object sender, EventArgs e)
            {
                vendorGrp.Show();
            }
    
            private void خروجToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }
    
            private void submitBtn_Click(object sender, EventArgs e)
            {
                SqlCommand command;
                string insert = @"insert into vendor(Address , Company_Name , Country , Email , Fax , Industry , Phone , Resume , ResumeWithUs , TypeOfItems , TypeOfServices , WebSite) 
                  values (@Address , @Company_Name , @Country , @Email , @Fax , @Industry , @Phone , @Resume , @ResumeWithUs , @TypeOfItems , @TypeOfServices , @WebSite) ";
    
                using (SqlConnection con = new SqlConnection(con_string))
                {
                    try
                    { 
                    con.Open();
    
                    command = new SqlCommand(insert, con);
                    command.Parameters.AddWithValue(@"Address", AddressTxt.Text);
                    command.Parameters.AddWithValue(@"Company_Name", CompanyTxt.Text);
                    command.Parameters.AddWithValue(@"Country", CompanyTxt.Text);
                    command.Parameters.AddWithValue(@"Email", EmailTxt.Text);
                    command.Parameters.AddWithValue(@"Fax", FaxTxt.Text);
                    command.Parameters.AddWithValue(@"Industry", IndustryTxt.Text);
                    command.Parameters.AddWithValue(@"Phone", TelephoneTxt.Text);
                    command.Parameters.AddWithValue(@"Resume",Resumetxt.Text );
                    command.Parameters.AddWithValue(@"ResumeWithUs", ResumeWustxt.Text);
                    command.Parameters.AddWithValue(@"TypeOfItems", ItemsTxt.Text);
                    command.Parameters.AddWithValue(@"TypeOfServices", servicetxt.Text);
                    command.Parameters.AddWithValue(@"WebSite", WebSiteTxt.Text);
                    
                    command.ExecuteNonQuery();
                    }
                    
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
    
                    Getdata("select * from vendor");
                    dataGridView1.Update();
                }
    
    
            }
    
            private void vendorGrp_Enter(object sender, EventArgs e)
            {
                dataGridView1.DataSource = bindingSource1;
            }
    
            private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
            {
    
            }
    
            
    
            private void toolStripMenuItem1_Click(object sender, EventArgs e)
            {
               
            }
    
            private void جستجویاطلاعاتToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                
            }
    
            private void DeleteBtn_Click(object sender, EventArgs e)
            {
                
    
                DataGridViewRow row = dataGridView1.CurrentCell.OwningRow;
                string value = row.Cells["id"].Value.ToString();
                string cname = row.Cells["Company_Name"].Value.ToString();
                string address = row.Cells["Address"].Value.ToString();
                DialogResult dialogresualt = MessageBox.Show("آیا از حذف رکورد مطمئن هستید","پیغام", MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                string DeleteStatment = "Delete from vendor where id = '" + value + "'";
                if(dialogresualt == DialogResult.Yes)
                {
                    using (SqlConnection con = new SqlConnection(con_string))
                    {
                        try
                        { 
                        con.Open();
                        SqlCommand com = new SqlCommand(DeleteStatment, con);
                        com.ExecuteNonQuery();
                            Getdata("select * from vendor");
                            dataGridView1.Update();
                              
                        }
    
                        catch(Exception ed)
                        {
                            MessageBox.Show(ed.Message);
                        }
                    }
                }
            }
    
            private void SearchBtn_Click(object sender, EventArgs e)
            {
                //WindowState = FormWindowState.Maximized;
                //vendorGrp.Width = this.Width;
                //dataGridView1.Width = this.Width -200;
    
                switch(searchcombo.SelectedItem.ToString())
                {
                    case "نام شرکت":
                        Getdata("select * from vendor where lower(Company_Name) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                    case "تلفن":
                        Getdata("select * from vendor where lower(Phone) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                    case "ایمیل":
                        Getdata("select * from vendor where lower(Email) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                    case "فکس":
                        Getdata("select * from vendor where lower(Fax) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                    case "وب سایت":
                        Getdata("select * from vendor where lower(WebSite) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                    case "زمینه فعالیت":
                        Getdata("select * from vendor where lower(TypeOfService) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                    case "صنعت":
                        Getdata("select * from vendor where lower(Industry) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                    case "محصولات":
                        Getdata("select * from vendor where lower(TypeOfItems) like '%" + searchtxt.Text.ToLower() + "%'");
                        break;
                }
                
                
            }
        }
    }



我尝试过的事情:

我几乎尝试了一切
例如在客户端pc



What I have tried:

i tried almost everything
like enabling all the sql server express services on client pc

推荐答案

string con_string = @"Data Source=.\SQLEXPRESS2014;Initial Catalog=hamkar_Vendor1;Integrated Security=True";


此路径.\ SQLEXPRESS2014"是本地存储,您需要将其更改为目标服务器.


This path ''.\SQLEXPRESS2014'' is to local storage, you need to change it to target server.

Getdata("select * from vendor where lower(Company_Name) like '%" + searchtxt.Text.ToLower() + "%'");


为什么要以此作为参数的用法?

不是您的问题的解决方案,而是您遇到的另一个问题.
切勿通过串联字符串来构建SQL查询.迟早,您将使用用户输入来执行此操作,这将打开一个名为"SQL注入"的漏洞的大门,这对您的数据库很危险,并且容易出错.
名称中的单引号会导致程序崩溃.如果用户输入的名称如"Brian O" Conner"可能会使您的应用程序崩溃,则这是一个SQL注入漏洞,而崩溃是最少的问题,这是恶意的用户输入,并且使用所有凭据将其提升为SQL命令.
SQL注入-Wikipedia [ ^ ]
SQL注入 [ ^ ]
SQL注入攻击示例 [ PHP:SQL注入-手册 [ SQL注入预防作弊表-OWASP [


Why are doing this as your about usage of parameters?

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O''Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


这篇关于如何在客户端PC上运行Windows Form SQL Server Express基于程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆