|
There's a client/server example on MSDN[^]. Be sure to check out the hyperlinks at the bottom of the page.
|
|
|
|
|
Thanks for this link, i already read this article, based on this, as i can understand is a console based server/client.
how i can whrite a simple command for server or client to get infos about printers.
for example i use this peace of code to retrieve local printers on a remote computer
private void GetPrinters()
{
string strServer = "\\\\" + this.textBox20.Text;
ConnectionOptions options = new ConnectionOptions();
options.Username = "xxxx";
options.Password = "xxxxx";
options.Authority = "NTLMDOMAIN:consilium";
ManagementScope myScope = new ManagementScope(strServer + "\\root\\cimv2", options);
SelectQuery oQuery = new SelectQuery("SELECT * FROM WIN32_Printer");
ManagementObjectSearcher oResults = new ManagementObjectSearcher(myScope, oQuery);
foreach (ManagementObject oItem in oResults.Get())
{
string strPrinterName = oItem.Properties["DriverName"].Value.ToString();
listBox2.Items.Add(strPrinterName.ToString().ToUpper());
}
}
i would like to execute this code on a remote computer and get the data back to my winform.
thank you in advance
|
|
|
|
|
tdcmystere wrote: i would like to execute this code on a remote computer and get the data back to my winform.
Then you will need to write something to do that communication, or use an existing solution
You could also adapt the console-example to suit your needs, hence the link. If that's too much trouble, then you'd have to map a network-drive from the server, and use a batch-file to write the result to a file on that network-share. E.g.;
MyCommand.exe > F:\thefile.txt
|
|
|
|
|
I am really new to C# .NET and SQL Server, I usually manage to find all my informations thanks to existing posts, but I have to admit I'm actually stuck and a bit lost with all the resources available.
I am actually developing a Windows Forms Application with Visual C# Express 2010 which would use (read/write) data from a SQL Server 2008 Express DB
I have created my DB with SQL Server Management Studio (2008 Express), I understand the instance is named ATLELAG786576\SQLEXPRESS My DB is called 'TEST'
Looking at my DB 'TEST' Properties in SQL Server Management Studio (2008 Express): Under Files, I am (ATLE\bneveux) the owner of the DB
Looking under Security, Logins, Mylogin (ATLE\bneveux) My default DB is 'TEST' Server roles are 'public' + 'sysadmin' User Mapping DB 'TEST' User 'dbo' Default Schema 'dbo'
In my C# application
app.config:
="1.0"="utf-8" <configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="connectionStringTestDb"
connectionString="Data Source=ATLELAG786576\SQLEXPRESS;Initial Catalog=D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf;Integrated Security=True;Connect Timeout=30;User Instance=False"
providerName="System.Data.SqlClient" />
</connectionStrings> </configuration>
dbConnection.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace SQLServerConnectionDemo
{
class dbConnection
{
public static SqlConnection newCon;
public static string connectionStringTestDb = ConfigurationManager.ConnectionStrings["connectionStringTestDb"].ConnectionString;
public static SqlConnection GetConnection()
{
newCon = new SqlConnection(connectionStringTestDb);
return newCon;
}
}
}
dbAccess.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace SQLServerConnectionDemo
{
class dbAccess
{
SqlConnection conn;
public dbAccess()
{
conn = dbConnection.GetConnection();
}
public void addEmployee(string Id, string Name, string Email)
{
if (conn.State.ToString() == "Closed")
{
conn.Open();
}
SqlCommand newCmd = conn.CreateCommand();
newCmd.Connection = conn;
newCmd.CommandType = CommandType.Text;
newCmd.CommandText = "INSERT INTO tblEmployees VALUES ('"+ Id +"','"+ Name +"','"+ Email +"')";
newCmd.ExecuteNonQuery();
}
}
}
in a form formEmployeeAdd.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SQLServerConnectionDemo
{
public partial class formEmployeeAdd : Form
{
dbAccess access = new dbAccess();
public formEmployeeAdd()
{
InitializeComponent();
}
private void btnInsert_Click(object sender, EventArgs e)
{
access.addEmployee(txtId.Text, txtName.Text, txtEmail.Text);
MessageBox.Show("Data successfully added");
}
}
}
And here the error message i always get when trying to run this process:
System.Data.SqlClient.SqlException (0x80131904): Cannot open database "D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf" requested by the login. The login failed. Login failed for user 'ATLE\bneveux'.
Note that I have never really been able to add my Data Source in Visual C# 2010 Express so I could manage the DB from VS, I always get the following error message:
Unable to open the physical file "D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf". Operating system error 32: "32(Le processus ne peut pas accéder au fichier car ce fichier est utilisé par un autre processus.)". An attempt to attach an auto-named database for file D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
Thank you for your expertise
Brice
|
|
|
|
|
|
Hi,
I have a text box in my form. I want to enter a pagraph in this text box, then store it as string (with XML format) into a table in SQL.
Because this is a paragraph, so the text value of the text box will include characters like: "\r\n" and "\r\n ".
But, I only can wirte and read "\n" of the text box in xml string, so I can't reload the original paragraph.
I used the below codes to write content of the text box:
strContent += (txtParagraph.Text != "") ? "<CONTENT>" + txtParagraph.Text + "</CONTENT>" : "<CONTENT />";
How can I write and read a below paragraph:
"Today is Monday.....
Tomorrow is Tuesday.
This month is November !"
Thanks and regards,
|
|
|
|
|
Just use string.Replace to change /n to /r/n
txtParagraph.text.Replace("\n","\r\n");
No more Mister Nice Guy... >: |
|
|
|
|
|
Thank you,
But the text box still only show contents in one line (not multi lines)
|
|
|
|
|
After returning from database?
No more Mister Nice Guy... >: |
|
|
|
|
|
Yes.
After get from database. I used the statement:
txtParagraph.Text = doc.SelectSingleNode("/CONTENT").InnerText;
But the textbox only shows in one line, like this: "This is the first line This is the second line This is the last line"
|
|
|
|
|
After returning from database you have to restore text to state you get it. So use string.Replace but in another direction.
textFromDatabase.Replace("/n","/r/n");
Why you cant add caret return in your xml? Replacing strings back and forth is not seems nice.
No more Mister Nice Guy... >: |
|
|
|
|
|
Thank you for your help.
I can do now, by using: txtContent.Text = txtContent.Text.Replace("\n", "\r\n");
P/s: Last, I used txtContent.Text.Replace("\n", "\r\n"), so it is not successful
|
|
|
|
|
Yes I thought of that. I did not include it in message before because entering '=' from my phone is really pain in the ass.
So everything is working fine?
No more Mister Nice Guy... >: |
|
|
|
|
|
Yes . Thank you very much
|
|
|
|
|
You're probably doing something wrong. I just tried it and had no trouble at all. Xml is perfectly capable of storing the text and giving it back.
You would need to show us the code you are using to determine what's wrong. Whatever the problem is, Replace isn't the best solution. Even if you think Replace is "good enough" for the particular program you're working on, it is not a good general solution and finding a better one will help you become a better developer.
P.S. What you posted will not produce well-formed XML if the user enters characters that aren't allowed. Try something like:
System.Xml.XmlNode nod = doc.CreateElement ( "Content" ) ;
nod.InnerText = this.textBox1.Text ;
Then write the nod.OuterXml to the database so any such characters get encoded properly.
modified 20-Nov-12 16:21pm.
|
|
|
|
|
Yes because every environment is storing new lines in the same way and it is always CR+LF.
He wrote clearly: Quote: "But, I only can wirte and read "\n" of the text box in xml string, so I can't reload the original paragraph."
Maybe his xmls are from some Unix-like system. Did you thought about that?
But you probably right he should write some kind of a xml-string-chunks-read-write-wrapper 500 lines long to deal with that.
No more Mister Nice Guy... >: |
|
|
|
|
|
n.podbielski wrote: I only can wirte and read
I read "can" as "know how to". If he has a Winforms app with a (multiline) TextBox and simply persists and restores the Text content as I understsand the question, he should have no trouble.
|
|
|
|
|
Ok let's pretend that this means that he can only know how to read text from text box with LF.
He added that code
strContent += (txtParagraph.Text != "") ? "<CONTENT>" + txtParagraph.Text + "</CONTENT>" : "<CONTENT />";
This read text from text box and ads xml tags, and makes string strContent. Where are this "know how to" read text with only LF? This read simple Text property form text box. Since text box
use CR+LF this read text with CR+LF? So why he wrote that he only "know how to" read only LF? Where is sense in that?
More. Line above text you quoted he wrote
Quote: Because this is a paragraph, so the text value of the text box will include characters like: "\r\n" and "\r\n ".
So he wrote that text box have CR+LF, then in next sentence wrote that he only "know how to" read LF and pasted code sample with reading text box with CR+LF chars.
Where is sense in that? There is non.
Obviously is writing some king of app to edits XML content with only LF, and from text box gets CR+LF.
You would get to the same if you would spend on it more than half a minute, think and do not reply with 'you are bad programmer because you like simple solution'
No more Mister Nice Guy... >: |
|
|
|
|
|
n.podbielski wrote: spend on it more than half a minute
I did, I wrote a whole database table (SQL Server 2008 R2) and Winforms app in an effort to reproduce the "problem". Did you?
I was unable to reproduce the problem, therefore there must be more information that the OP left out.
For instance, he doesn't show how he gets the strContent into and out of the database, that could be where the value gets messed up. Without more information, a proper answer can't be provided.
|
|
|
|
|
You are never humble, do you. Who are you to to tell what answer is "proper"? Do you know everything?
If someone is happy with solution do you have to force him to change his mind?
1. Text box returns "\r\n" in content
2. Content must be inserted into xml without "\r"
What is "left out"? That's the problem.
What about solution.
Why I waste my time discussing anything with you, self proclaiming, uber-programmer. Maybe in you world overcomplicating thinks is good thing. Not in mine.
No more Mister Nice Guy... >: |
|
|
|
|
|
n.podbielski wrote: 2. Content must be inserted into xml without "\r"
Ah, so that's how you're interpreting it. I interpreted it as the OP wants to store the \r , but for some reason it's getting lost along the way. As I said, I was unable to duplicate the problem. If, as you say, he wants to not store the \r yet get it back again (and I have no idea why that would be), then I would still have better suggestions than Replace.
At any rate, until the OP responds with a clearer explanation of the situation you and I could continue to debate it and neither of us wants that.
n.podbielski wrote: Maybe in you world overcomplicating thinks is good thing. Not in mine.
Solving the problem rather than sweeping it under the rug to cause trouble down the line is what I consider a good thing.
|
|
|
|
|
Hi PIEBALDconsult, thank you.
I think you are right, it will not produce well-formed XML. I only store my text in tags format (similar XML), then I used XML class to read it. For example:
<MAINCONTENT>
<SUBCONTENT1>text...
</SUBCONTENT1>
<SUBCONTENT2>abc123
</SUBCONTENT2>
</MAINCONTENT>
I used below statements to read content:
<b>System.Xml.XmlReader reader = XmlReader.Create(new StringReader(strContentFromDatabase));
XmlDocument doc = new XmlDocument();
doc.Load(reader);
txtContent.Text = doc.SelectSingleNode("/MAINCONTENT/SUBCONTENT1").InnerText;</b>
|
|
|
|
|
Thanks for the clarification.
I wonder whether or not the StringReader is eating your whitespace characters (I'll have to try it).
Try doc.LoadXml (strContentFromDatabase) ; which I used in my tests.
P.S. Testing shows that the StringReader eats the \r !
Edit: It's the XmlReader, not the StringReader.
modified 21-Nov-12 9:36am.
|
|
|
|
|
Oh, great. It is successful with your way (not eat whitespace).
Thank you so much
|
|
|
|
|