简体   繁体   中英

Linked database sql server 2008 to visual studio 2010

I'm doing a software which can manage electronics component quantity. My software is linked to a first DB (T_importReel) that will have all the new reels component when the factory receive them. The machine that uses component is counting how many it has used in each reels and marks the count in its own DB (SiplaceSetupCenter). I'm using an Unique ID for each reel so T_importReel and SiplaceSetupCenter are using that Unique ID to differentiate each bobine. Basically, I have in the two DB two columns that are the same.

Now I would like to link the two DB with a simple SQL request. This is supposed to be simple : for every Unique ID where the quantity is different between the two DB, T_importReel changes its quantity for the Unique ID concerned.

But when I've tried to do so, my soft didn't access DB_center... How can I enable my soft to access SetUpCenter? What do I do to update T_importreels? Thank you in advance.

 private void BT_croiserlesdonnées_Click(object sender, EventArgs e)
  {

     SqlConnection con = new SqlConnection("Server=SIPLACESERVER;Database=SiplaceSetupCenter;User=**;Password=****;");
      con.Open();
      // using (SqlCommand command = new SqlCommand(
      //      "SELECT packagingunitid,Quantity FROM PackagingUnit",
      //      con))
      //using (SqlDataReader reader = command.ExecuteReader())
      //      {
      //          while (reader.Read())
      //          {
      //              for (int i = 0; i < reader.FieldCount; i++)
      //              {
      //                  Console.WriteLine(reader.GetValue(i));
      //              }
      //              Console.WriteLine();
      //          }
      //      }

     string queryString = 
          "Select Quantity FROM PackagingUnit, T_ImportReels WHERE PackagingUnit.id = T_ImportReels.id";
      SqlCommand command = new SqlCommand(
        queryString, con);

    SqlDataReader reader = command.ExecuteReader();
    try
    {
        while (reader.Read())
        {
            Console.WriteLine(String.Format("{0}, {1}",
                reader[0], reader[1]));
        }
    }
    finally
    {
        // Always call Close when done reading.
        reader.Close();
    }

      //_siplaceSetupCenter = new SiplaceSetupCenterEntities();
      //string queryString = @"SELECT * FROM PackagingUnit";

      //using (SiplaceSetupCenterEntities context = new SiplaceSetupCenterEntities())
      //{


      //    //ObjectQuery<PackagingUnit> contactQuery =_siplaceSetupCenter.CreateQuery<PackagingUnit>(queryString);
      //    // new ObjectParameter("fn", "Frances"));
      //    Console.WriteLine("on y go"); Console.WriteLine("on y go"); Console.WriteLine("on y go"); Console.WriteLine("on y go");
      //    var tableresult = _siplaceSetupCenter.PackagingUnit.FirstOrDefault(id => id.PackagingUnitId == "100717132736");



          //var contactQuery = from contact in context.PackagingUnit
          //                   where contact.PackagingUnitId == "200717100643"
          //                   select contact;
          //// Iterate through the collection of Contact items.
          //foreach (PackagingUnit result in contactQuery)
          //    Console.WriteLine("First Name: {0}, Last Name: {1}",result.PackagingUnitId, result.Quantity);
          //Console.WriteLine(table_result.Count); Console.WriteLine(table_result.Count); Console.WriteLine(table_result.Count); Console.WriteLine(table_result.Count);
          //foreach (var value in table_result)
          //{
          //    Console.WriteLine(value); Console.WriteLine(value); Console.WriteLine(value); Console.WriteLine(value);
          //    Console.WriteLine("a"); Console.WriteLine("a"); Console.WriteLine("a"); Console.WriteLine("a");
          //}

          //string dataid = tableresult.PackagingUnitId;
          //Console.WriteLine(dataid);
      }
  }

I

I don't know how it could help you? You can make two connections that each one connects to each database then select your Data from two Db. In this situation you have 2 DataTable/ SqlDataReader objects. Now You can compare these Datatables/SqlDataReader row by row with each others or combine them into a single datatable/SqlDataReader or update each one base another.

Sql Server's name should be sth like this SERVERNAME\\INSTANCENAME. But your server name is this: SIPLACESERVER/SIPLACE_2012EX.

If you use "\\" character in your server name, in connection string change it into "\\\\". i mean change SERVERNAME\\INSTANCENAME into SERVERNAME\\\\INSTANCENAME .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM