Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How data can be fetched from SQL Server in SparkCLR?
You could use the following SparkCLR code as a reference to use C# for loading Spark DataFrame from the data in SQL Server, Azure SQL Database or any other JDBC compliant datasource.
//C# sample to load SQL Server data as Spark DataFrame using JDBC
var sparkConf = new SparkConf();
var sparkContext = new SparkContext(sparkConf);
var sqlContext = new SqlContext(sparkContext);
var dataFrame = sqlContext.Read()
.Jdbc("jdbc:sqlserver://localhost:1433;databaseName=Temp;;integratedSecurity=true;", "xyz",
new Dictionary<string, string>());
dataFrame.ShowSchema();
var rowCount = dataFrame.Count();
Console.WriteLine("Row count is " + rowCount);
Few things to note:
This sample code uses Microsoft JDBC driver. If you use a different driver or JDBC datasource you need to update the url
You need to include the driver jar file when submitting your SparkCLR job
SparkCLR project for this sample is available # https://github.com/Microsoft/SparkCLR/tree/master/examples/JdbcDataFrame
My recommendation is to use JDBC to connect to sql server then query against the Dataframe.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last month.
Improve this question
Some time we need to import or insert all the CSV files located in a directory into database. The following C# code will insert all the files in a directory into database: const string CSV_CONNECTIONSTRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="{0}";Extended Properties="text;HDR=YES;FMT=Delimited""
As we don't have a lot ot information, we just can give you some tips in order to achieve what you want:
You need to install a NuGet package that provides CSV reading functionality. One sample of it it's "CsvHelper library" CSV Library
Then just use it, you will in the docs how to use it, but one sample of it would be:
void Main()
{
using (var reader = new StreamReader("path\\to\\file.csv"))
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
var records = csv.GetRecords<dynamic>();
foreach (var record in records) {
// whatever u want
}
}
}
Hope that helps.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I Already connected my SQL database with visual studio via server explorer And i filled some tables with values.
Now, I want to access/search through a table from my SQL database in my visual studio project.
For example: in my table i have products from different categories and i only want to display products from a specific category
But, how can in do that?
There is many ways by which you can access.If you are using connected mode Than you have to create object for connection string
SqlConnection con = new SqlConnection(#"Data Source=nameof_server;Initial Catalog=name_of_database;Integrated Security=True");
SqlCommand cmd = new SqlCommand("sql_command",con);
con.open()
//perform action based on your requirment
ExecuteNonQuery() // Use this for insert,update,delete
ExecuteScalar() // Use this for select single column
ExecuteReader() // Use this for select multiple records
con.close()
Please check this link
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I was wondering if can I connect to sql database from another computer without installing sql server (is this possible). I am creating a c# wpf program that would get and insert data in sql database that is placed in my PC, in all computers where this program is installed using internet. Should I use in connection string my internet IP, or what, because I’m confused?
My connection string
String connString = #"Network Library=dbmssocn;
Network Address=127.0.0.1,1433;
Integrated security=SSPI;
Initial Catalog=db";
Also i forgot to say that i'm new in programing area, and i want to connect to sql database through lan connection
Assuming that your remote database (on your other computer) accepts remote connections, you can accomplish this via a traditional ADO.NET connection or you could use an ORM like Entity Framework to handle the connection.
You can see an example below that takes advantage of the SqlConnection class which does exactly what it's name implies :
using(var sqlConnection = new SqlConnection("Your Connection String"))
{
var query = "Your Query";
using(var sqlCommand = new SqlCommand(query,sqlConnection))
{
sqlConnection.Open();
// Execute your query here using sqlCommand.ExecuteNonQuery()
// or some other execution method
}
}
You would just need to ensure that your actual connection string to target your remote database is correct.
Yes, it is possible. You could use ADO.NET or Entity Framework to accomplish this goal. However, I would recommend setting up a web api on the sever you have hosting the database, which you could call to get/post data. I personally wouldn't hard code any connection strings in the program you plan to deploy to end user workstations. Just call the web api and let your web api talk to the database.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Working on a legacy application where Data Access repository always returns a data set. Changing to any ORM frameworks is not an option for me at this point, looking for best options to map result set to a CLR type
I know of 2 easy ways to do this.
1 - Use Dapper.NET
const string query = "SELECT * FROM Users";
return connection.Query<User>(query);
With Dapper you don't even have to worry about getting the DataTable, just query the SqlConnection and get your type back.
2 - Use Automapper:
List<User> users = AutoMapper.Mapper.DynamicMap<IDataReader, List<User>>(
sourceDataTable.CreateDataReader());
Automapper can map your DataTable to your type.
Both methods require your class to have property names that match the column names in your source data.
I think they are both available as NuGet packages
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm just starting out with server side code and Visual Studio with C#, but something I can't figure out with my googling is how do people use C# and Visual Studio to access a database and pull results and display them on the html documment?
I have an SQL Server 2012, I believe I know how to connect to the database itself, but unless I use classic ASP I don't understand how to pull it from the database and display it using C# and or ASP.NET.
I suggest you start out with Ado.Net. Once you have a good understanding of the principles, look into Entity Framework.
Robin Nadeau is right, what you'll probably want to start with is Ado.Net controls. In reference to your example, how I would go about it is using a DataGridView and binding it with a BindingSource + DataSet. When you drop a DataGridView on the form, it will give you a chance to run through a wizard to easily set up the BindingSource and Dataset.
To filter stuff from there, you'll need to hook up the buttons etc. with code such as this:
bindingSource1.Filter = string.Format("VideoTitle LIKE '{0}'", txtSearch.Text);
Hopefully that helps you get started.
If you are a beginner and don't have enough time to learn a complete framework. I would suggest to use the WebMatrix.Data Wrapper. Life's simple with it. 6 Commands is all you need to know to make 90% of the applications. Just install the Nuget Package and you're good to go.
Those 6 Commands are
1. Open & Close Connection
var db=Database.Open("name of connection string in web.config");
db.Close();
2. SELECT Query
foreach(var row in db.Query("SELECT * FROM tablename")
{
servervariable1=row.column1;
}
3. SELECT Single Row
var row=db.QuerySingle("SELECT * FROM tablename WHERE Key=#0",PassedVariable);
if(row!=null)
{
// do the operation
}
else
{
// handle code if the row is not found
}
4. SELECT Single Value
try{
datatype variable=db.QueryValue("SELECT columnname FROM tablename WHERE Key=#0",Key);
}
catch{
// handle code if row doesn't exists
}
5. INSERT Query
db.Execute("INSERT INTO tablename(column1,column2) Values(#0,#1)",inputvar1,inputvar2);
6. DELETE Query
db.Execute("DELETE * FROM tablename WHERE Key=#0",key);